我创建了一个带有一些按钮的matplotlib
图,这可以改变绘图参数,从而更新绘图。它工作得很好,但我无法理解放置按钮(或任何其他matplotlib小部件)的逻辑。
例如,在matplotlib official button example中,它通过调用plt.axes()
放置小部件,然后将ax元素传递给Button构造函数:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
freqs = np.arange(2, 20, 3)
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.2)
t = np.arange(0.0, 1.0, 0.001)
s = np.sin(2*np.pi*freqs[0]*t)
l, = plt.plot(t, s, lw=2)
class Index(object):
ind = 0
def next(self, event):
self.ind += 1
i = self.ind % len(freqs)
ydata = np.sin(2*np.pi*freqs[i]*t)
l.set_ydata(ydata)
plt.draw()
def prev(self, event):
self.ind -= 1
i = self.ind % len(freqs)
ydata = np.sin(2*np.pi*freqs[i]*t)
l.set_ydata(ydata)
plt.draw()
callback = Index()
axprev = plt.axes([0.7, 0.05, 0.1, 0.075])
axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
bnext = Button(axnext, 'Next')
bnext.on_clicked(callback.next)
bprev = Button(axprev, 'Previous')
bprev.on_clicked(callback.prev)
plt.show()
正如matplotlib
的文档中所述,axes
方法使用元素[xmin xmax ymin ymax]
获取列表作为输入。
由于Axes
元素(如果错误,请更正)只是轴和图表出现的区域,设置[xmin xmax ymin ymax]
不是一种可以理解的正确放置方式。
我不知道问题是这个例子是否过于简单,还有另一种更适合的方法来放置Figure
的所有内容,或者如果我不理解如何放置(
任何帮助?