如何解决在matplotlib中使用滑块不更新子图
我不确定滑块出现了什么问题。一旦修改了滑块值,它就不会更新。我被困了很长时间,任何线索都会有所帮助。预先感谢!
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider,Button,RadioButtons
import matplotlib.animation as animation
plt.cla()
n = 1000
x1 = np.random.normal(5,1,n)
x2 = np.random.gamma(2,1.5,n)
x3 = np.random.exponential(2,n)
x4 = np.random.uniform(0,10,n)
fig,((ax1,ax2),(ax3,ax4)) = plt.subplots(2,2,sharey=True,sharex = True)
plt.axis([0,600])
axs = [ax1,ax2,ax3,ax4]
ax1.hist(x1,density=False,bins=10,alpha=0.5)
ax2.hist(x2,alpha=0.5)
ax3.hist(x3,alpha=0.5)
ax4.hist(x4,alpha=0.5)
axcolor = 'lightgoldenrodyellow'
axfreq = plt.axes([.2,.01,.65,.02],facecolor=axcolor)
sample_slider = DiscreteSlider(axfreq,'Sample Size \n (100-1000)',100,1000,valinit=1000)
def update(val):
n = sample_slider.val
print(n)
plt.annotate('n = {}'.format(n),[9,15])
ax1.cla()
ax2.cla()
ax3.cla()
ax4.cla()
x_1 = np.random.normal(5,n)
x_2 = np.random.gamma(2,n)
x_3 = np.random.exponential(2,n)
x_4 = np.random.uniform(0,n)
plt.axis([0,500])
ax1.hist(x_4,alpha=0.5)
ax2.hist(x_3,alpha=0.5)
ax3.hist(x_2,alpha=0.5)
ax4.hist(x_1,alpha=0.5)
fig.canvas.draw_idle()
sample_slider.on_changed(update)
plt.show()
离散滑块-我正在上课
DiscreteSlider(Slider)类: “”“具有离散步骤的matplotlib滑块小部件。”“” def init (自身,* args,** kwargs): “”“与Slider相同。初始化,但“增量” kwarg除外。 “ increment”(增量)指定将使滑块离散化的步长 至。””” self.inc = kwargs.pop('increment',50) 滑块。 init (自身,* args,** kwargs) self.val = 1
def set_val(self,val):
discrete_val = int(val / self.inc) * self.inc
# We can't just call Slider.set_val(self,discrete_val),because this
# will prevent the slider from updating properly (it will get stuck at
# the first step and not "slide"). Instead,we'll keep track of the
# the continuous value as self.val and pass in the discrete value to
# everything else.
xy = self.poly.xy
xy[2] = discrete_val,1
xy[3] = discrete_val,0
self.poly.xy = xy
self.valtext.set_text(self.valfmt % discrete_val)
if self.drawon:
self.ax.figure.canvas.draw()
self.val = val
if not self.eventson:
return
for cid,func in self.observers.items():
func(discrete_val)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。