我正在创建一个交互式绘图,其中包含一些线条和注释等。一个最小的工作示例是:
def make_plot(scale_factor):
fig = plt.figure(figsize=(8,8)) # create a figure object
ax = fig.add_subplot(1, 1, 1)
x = np.linspace(0,10)
y = np.sin(x)*scale_factor
ax.set_ylim(0,10)
ax.axhline(2,c='k')
plt.plot(x,y)
def make_interactive_plot():
scale_factor_slider = wg.FloatSlider(value=1,
min=0,
max=5,
step=0.1,
description=r'Scaling Factor')
wg.interact(make_plot,
scale_factor = scale_factor_slider)
make_interactive_plot()
每次我用滑块改变一个值。我只想更新Line2D对象。这是matplotlib.animation功能。我想这样做:
def update_plot(line_obj,scale_factor):
print(scale_factor)
x = np.linspace(0,10)
y = np.sin(x)*scale_factor
line_obj.set_data(x,y)
def make_interactive_plot():
scale_factor_slider = wg.FloatSlider(value=1,
min=0,
max=5,
step=0.1,
description=r'Scaling Factor')
wg.interact(update_plot,
line_obj = line_obj,
scale_factor = scale_factor_slider)
# Initialise plot just once
fig = plt.figure(figsize=(8,8)) # create a figure object
ax = fig.add_subplot(1, 1, 1)
ax.set_ylim(0,10)
ax.axhline(2,c='k')
line_obj, = ax.plot([],[])
make_interactive_plot()
不幸的是,这引发了:
ValueError: cannot be transformed to a widget