• <i id='DHtLX'><tr id='DHtLX'><dt id='DHtLX'><q id='DHtLX'><span id='DHtLX'><b id='DHtLX'><form id='DHtLX'><ins id='DHtLX'></ins><ul id='DHtLX'></ul><sub id='DHtLX'></sub></form><legend id='DHtLX'></legend><bdo id='DHtLX'><pre id='DHtLX'><center id='DHtLX'></center></pre></bdo></b><th id='DHtLX'></th></span></q></dt></tr></i><div id='DHtLX'><tfoot id='DHtLX'></tfoot><dl id='DHtLX'><fieldset id='DHtLX'></fieldset></dl></div>
    <tfoot id='DHtLX'></tfoot>
  • <legend id='DHtLX'><style id='DHtLX'><dir id='DHtLX'><q id='DHtLX'></q></dir></style></legend>

      • <bdo id='DHtLX'></bdo><ul id='DHtLX'></ul>
    1. <small id='DHtLX'></small><noframes id='DHtLX'>

        使用滑块选择要显示的图形

        时间:2024-08-10

          <bdo id='MU1s8'></bdo><ul id='MU1s8'></ul>

          <small id='MU1s8'></small><noframes id='MU1s8'>

              <tbody id='MU1s8'></tbody>
              <tfoot id='MU1s8'></tfoot>
              <i id='MU1s8'><tr id='MU1s8'><dt id='MU1s8'><q id='MU1s8'><span id='MU1s8'><b id='MU1s8'><form id='MU1s8'><ins id='MU1s8'></ins><ul id='MU1s8'></ul><sub id='MU1s8'></sub></form><legend id='MU1s8'></legend><bdo id='MU1s8'><pre id='MU1s8'><center id='MU1s8'></center></pre></bdo></b><th id='MU1s8'></th></span></q></dt></tr></i><div id='MU1s8'><tfoot id='MU1s8'></tfoot><dl id='MU1s8'><fieldset id='MU1s8'></fieldset></dl></div>

              • <legend id='MU1s8'><style id='MU1s8'><dir id='MU1s8'><q id='MU1s8'></q></dir></style></legend>

                1. 本文介绍了使用滑块选择要显示的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我想通过读取三个.xlsx文件来显示三个模拟。 接下来,我想设计一个滑块来选择要显示的模拟。 如果我将滑块移到0,那么0将是函数";update()";的输入。将显示第一个模拟。

                  代码如下:

                  import numpy as np
                  import matplotlib.pyplot as plt
                  from matplotlib import cm
                  from matplotlib.widgets import Slider
                  import pandas as pd
                  import ipywidgets as wg
                  
                  # input files
                  
                  rm = pd.read_excel("test_3d.xlsx", header = None)
                  rm1 = pd.read_excel("test_3d1.xlsx", header = None)
                  rm2 = pd.read_excel("test_3d2.xlsx", header = None)
                  rec = np.shape(rm)
                  X = np.arange(1,rec[1]+1,1)
                  Y = np.arange(1,rec[0]+1,1)
                  x , y = np.meshgrid(X,Y)
                  
                  
                  # Set 3D plots
                  
                  fig = plt.figure()
                  ax1 = fig.add_axes([0, 0, 1, 0.8], projection = '3d')
                  
                  
                  # Choose which 3D plots to show
                  
                  def update(val):
                      if val == 0:
                          ax1.cla()
                          ax1.plot_surface(x, y, rm, cmap = cm.coolwarm, linewidth = 0, antialiased = False)        
                      elif val == 1:
                          ax1.cla()
                          ax1.plot_surface(x, y, rm1, cmap = cm.coolwarm, linewidth = 0, antialiased = False)
                      elif val == 2:
                          ax1.cla()
                          ax1.plot_surface(x, y, rm2, cmap = cm.coolwarm, linewidth = 0, antialiased = False)      
                      
                  ax1.set_zlim(-110, -80)
                  
                  # Design a slider to choose which simulation to show 
                  
                  slider = wg.IntSlider(value=1, min=0, max=2, description='this is slider')
                  slideroutput = wg.Output()
                  display(slider, slideroutput)
                  
                  
                  numberonslider = []
                  def on_value_change(change):
                  
                      with slideroutput:
                          numberonslider.append(change['new'])
                          print(numberonslider[-1])
                      
                      
                  ddd = slider.observe(on_value_change, names='value')
                  update(ddd)
                  

                  如果我移动滑块,";ddd";会显示0、1或2的列表。

                  但是,3D模拟不会显示。如何修改代码?

                  推荐答案

                  我正在使用JupyterLab。我需要%matplotlib widget用于任何类型的交互式matplotlib绘图。下面的代码运行正常,但是没有%matplotlib widget就无法运行。

                  警告:%matplotlib widgetimport matplotlib.widget

                  不同

                  您没有提供任何示例数据,所以我只是编造了一些数据。基本上您的代码结构不正确,if部分应该在def on_value_change(change):中。请参阅以下代码:

                  import numpy as np
                  import matplotlib.pyplot as plt
                  from matplotlib import cm
                  import ipywidgets as wg
                  %matplotlib widget
                  
                  X = np.arange(5)
                  Y = np.arange(5)
                  x, y = np.meshgrid(X, Y)
                  rm = np.sin(x)
                  rm1 = np.cos(x)
                  rm2 = y
                  
                  fig = plt.figure()
                  ax1 = fig.add_axes([0, 0, 1, 0.8], projection = '3d')
                  
                  # Design a slider to choose which simulation to show 
                  slider = wg.IntSlider(value=1, min=0, max=2, description='this is slider')
                  slideroutput = wg.Output()
                  display(slider, slideroutput)
                  
                  ax1.cla()
                  plt.title(f"This is slider number {str(slider.value)}")
                  ax1.plot_surface(x, y, rm1, cmap = cm.coolwarm, linewidth = 10, antialiased = False)
                  
                  numberonslider = []
                  def on_value_change(change):
                      with slideroutput:
                          numberonslider.append(change['new'])
                          if numberonslider[-1] == 0:
                              ax1.cla()
                              plt.title(f"This is slider number {str(slider.value)}")
                              ax1.plot_surface(x, y, rm, cmap = cm.coolwarm, linewidth = 10, antialiased = False)
                          elif numberonslider[-1] == 1:
                              ax1.cla()
                              plt.title(f"This is slider number {str(slider.value)}")
                              ax1.plot_surface(x, y, rm1, cmap = cm.coolwarm, linewidth = 10, antialiased = False)
                          elif numberonslider[-1] == 2:
                              ax1.cla()
                              plt.title(f"This is slider number {str(slider.value)}")
                              ax1.plot_surface(x, y, rm2, cmap = cm.coolwarm, linewidth = 10, antialiased = False)
                      
                  slider.observe(on_value_change, names='value')
                  

                  输出:

                  如果我的解释不清楚,请再次询问。

                  这篇关于使用滑块选择要显示的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:根据其中一段视频重建Veritasium的情节 下一篇:类型错误:数组数据类型(&amp;#39;对象&amp;#39;)和格式说明符(&amp;#39

                  相关文章

                  <legend id='6tjyT'><style id='6tjyT'><dir id='6tjyT'><q id='6tjyT'></q></dir></style></legend>

                  <tfoot id='6tjyT'></tfoot>
                  • <bdo id='6tjyT'></bdo><ul id='6tjyT'></ul>

                2. <i id='6tjyT'><tr id='6tjyT'><dt id='6tjyT'><q id='6tjyT'><span id='6tjyT'><b id='6tjyT'><form id='6tjyT'><ins id='6tjyT'></ins><ul id='6tjyT'></ul><sub id='6tjyT'></sub></form><legend id='6tjyT'></legend><bdo id='6tjyT'><pre id='6tjyT'><center id='6tjyT'></center></pre></bdo></b><th id='6tjyT'></th></span></q></dt></tr></i><div id='6tjyT'><tfoot id='6tjyT'></tfoot><dl id='6tjyT'><fieldset id='6tjyT'></fieldset></dl></div>

                    <small id='6tjyT'></small><noframes id='6tjyT'>