对于那些想要在条形图或绘图图之间切换的人(适用于两种类型的图表)。我也认为这样做更干净。
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
class Index(object):
def start(self, event=None):
ax.clear()
x_values = [1, 2, 3, 4, 5]
y_values = [3, 5, 2, 19, 1]
ax.bar(x_values, y_values)
plt.draw()
def next(self, event):
ax.clear()
x_values = [1, 2, 3, 4, 5]
y_values = [20, 15, 10, 5, 1]
ax.bar(x_values, y_values)
plt.draw()
def prev(self, event):
ax.clear()
x_values = [1, 2, 3, 4, 5]
y_values = [1, 5, 10, 15, 20]
ax.bar(x_values, y_values)
plt.draw()
ax = plt.gca()
callback = Index()
callback.start()
axprev = plt.axes([0.59, 0.002, 0.1, 0.075])
bprev = Button(axprev, 'Previous')
bprev.on_clicked(callback.prev)
axstart = plt.axes([0.7, 0.002, 0.1, 0.075])
bstart = Button(axstart, 'Start')
bstart.on_clicked(callback.start)
axnext = plt.axes([0.81, 0.002, 0.1, 0.075])
bnext = Button(axnext, 'Next')
bnext.on_clicked(callback.next)
plt.show()