2
我想创建一个具有三个参数的多维函数的交互式matplotlib图变化。问题是参数可能会在很大的范围内变化,所以我宁愿不使用滑块,而是直接键入我想要的值。基本上,我想重新创建下面的典型的例子,其中滑块代替我想文本框,其中我可以输入参数通过文本框交互式matplotlib图
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.35)
t = np.arange(0.0, 1.0, 0.001)
a0 = 5
f0 = 3
s = a0*np.sin(2*np.pi*f0*t)
l, = plt.plot(t,s, lw=2, color='red')
plt.axis([0, 1, -10, 10])
ax.plot(t,t)
axcolor = 'lightgoldenrodyellow'
axamp = plt.axes([0.25, 0.25, 0.65, 0.03], axisbg=axcolor)
axfreq = plt.axes([0.25, 0.2, 0.65, 0.03], axisbg=axcolor)
sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0)
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)
def update(val):
amp = samp.val
freq = sfreq.val
l.set_ydata(amp*np.sin(2*np.pi*freq*t))
fig.canvas.draw_idle()
sfreq.on_changed(update)
samp.on_changed(update)
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
def reset(event):
sfreq.reset()
samp.reset()
button.on_clicked(reset)
plt.show()
也看到了这个问题:https://stackoverflow.com/questions/43973758/how-do-i-make-matplotlib-open-a -box-for-user-comments – ImportanceOfBeingErnest
Matplotlib即将推出TextBox Widget(可能在版本2.1(?))。在[本例中]查看它的用法(https://matplotlib.org/devdocs/gallery/widgets/textbox.html)。 – ImportanceOfBeingErnest