从函数调用时,matplotlib滑块不起作用 [英] matplotlib slider not working when called from a function
本文介绍了从函数调用时,matplotlib滑块不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,其中显示了带有倾斜条的图形
I have the following code, which shows a graph with a slinding bar
from matplotlib.widgets import Slider
import matplotlib.pyplot as plt
from numpy import arange, sin, pi
t = arange(0.0, 10.0, 0.01)
fig =plt.figure(figsize = [30,20])
ax = plt.subplot(111)
plt.plot(t, sin(t*10))
plt.ylim((-2,2))
plt.xlim((0,1))
plt.tight_layout()
axzoom= plt.axes([0.15, 0.05, 0.65, 0.03])
szoom = Slider(axzoom, 'Window', 1, 2)
def update(val):
ax.set_xlim([val,val+1])
fig.canvas.draw_idle()
szoom.on_changed(update)
plt.show()
我想从一个函数中调用它,即:
I want to call it from a function, that is:
def myplot():
(... same code as above)
问题是,当我这样做时,斜杠不再起作用.对我可能做错的事情有任何想法吗?
The problem is, when I do that the slinding bar does not work anymore. Any idea of what I could be doing wrong?
我正在尝试从Spyder中的iPython执行它. 感谢您的任何帮助.
I am trying to execute it from iPython within Spyder. Thank you for any help.
推荐答案
基于此您需要将滑块保持在全局范围内.
Based on this you need to keep the sliders around globally.
您的示例仅需稍作调整:
Your example would only need to be slightly adjusted:
def myplot():
(... same code as above)
return szoom
keep_sliders = myplot()
这篇关于从函数调用时,matplotlib滑块不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文