使用滑块选择要显示的图形
Use slider to choose which graph to show
假设我想通过读取三个 .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 图,我都需要 %matplotlib widget
。下面的代码工作正常,但如果没有 %matplotlib widget
.
将无法工作
警告:%matplotlib widget
与import 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')
输出:
如果我的解释不清楚,请再次询问。
假设我想通过读取三个 .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 图,我都需要 %matplotlib widget
。下面的代码工作正常,但如果没有 %matplotlib widget
.
警告:%matplotlib widget
与import 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')
输出:
如果我的解释不清楚,请再次询问。