在 FigureCanvas 上显示的条形图动画中标记轴时遇到问题
Having problems labelling axes in a barplot animation shown on a FigureCanvas
我一直在使用 中概述的动画方法来创建动画并将它们放置在 tkinter FigureCanvas 上。
我很难以 y 轴刻度标签按我希望的方式显示一系列条形图的动画。我的动画将有 100 帧,每帧由一个使用四个 x 值的条形图组成。数据存储在一个 100×4 的矩阵中,其条目是随机的并且介于 0 和 1 之间。动画的每一帧都出现在 FigureCanvas 上。下面是我到目前为止所做的总结,其中 Player
是上面 link 中定义的 class。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from tkinter import Tk, TOP, BOTH
root=Tk()
root.geometry('1000x1000')
fig=Figure()
# Place canvas on figure. Each frame of the animation will place a barplot on the
#canvas.
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=1)
# Random 100-by-4 matrix, each row of which corresponds to an instant in time.
M=np.random.rand(100,4)
labels=['a','b','c','d']
num_times=M.shape[0]
def update_bar(i):
ax=fig.add_subplot(111)
ax.bar(labels,list(M[i,:]))
# Want y ticks to be labelled 0, .2, .4, .6, .8 for each frame.
ax.set_yticks(np.arange(0, 1, step=0.2))
ani = Player(fig, update_bar, maxi=num_times)
root.mainloop()
当我播放动画时,y 轴标签变得一团糟,因为在每一帧中都重新创建了刻度线。 (参见显示第八帧的图像。)
有没有办法让我在动画开始之前一次性设置y-tickmark标签?
你应该移动这条线:
ax = fig.add_subplot(111)
out of update_bar
函数:您不需要在每次迭代中使用新的 ax
。
然后,在 update_bar
函数中你应该添加这一行:
ax.cla()
为了抹去之前的剧情
最后,我建议添加这一行:
ax.set_ylim(0, 1)
在update_bar
函数中,为了保持固定的y轴限制。
完整代码
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from tkinter import Tk, TOP, BOTH
import matplotlib
from matplotlib.animation import FuncAnimation
import mpl_toolkits.axes_grid1
root=Tk()
root.geometry('1000x1000')
fig=Figure()
# Place canvas on figure. Each frame of the animation will place a barplot on the
#canvas.
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=1)
# Random 100-by-4 matrix, each row of which corresponds to an instant in time.
M=np.random.rand(100,4)
labels=['a','b','c','d']
num_times=M.shape[0]
class Player(FuncAnimation):
def __init__(self, fig, func, frames=None, init_func=None, fargs=None,
save_count=None, mini=0, maxi=100, pos=(0.125, 0.92), **kwargs):
self.i = 0
self.min=mini
self.max=maxi
self.runs = True
self.forwards = True
self.fig = fig
self.func = func
self.setup(pos)
FuncAnimation.__init__(self,self.fig, self.func, frames=self.play(),
init_func=init_func, fargs=fargs,
save_count=save_count, **kwargs )
def play(self):
while self.runs:
self.i = self.i+self.forwards-(not self.forwards)
if self.i > self.min and self.i < self.max:
yield self.i
else:
self.stop()
yield self.i
def start(self):
self.runs=True
self.event_source.start()
def stop(self, event=None):
self.runs = False
self.event_source.stop()
def forward(self, event=None):
self.forwards = True
self.start()
def backward(self, event=None):
self.forwards = False
self.start()
def oneforward(self, event=None):
self.forwards = True
self.onestep()
def onebackward(self, event=None):
self.forwards = False
self.onestep()
def onestep(self):
if self.i > self.min and self.i < self.max:
self.i = self.i+self.forwards-(not self.forwards)
elif self.i == self.min and self.forwards:
self.i+=1
elif self.i == self.max and not self.forwards:
self.i-=1
self.func(self.i)
self.fig.canvas.draw_idle()
def setup(self, pos):
playerax = self.fig.add_axes([pos[0],pos[1], 0.22, 0.04])
divider = mpl_toolkits.axes_grid1.make_axes_locatable(playerax)
bax = divider.append_axes("right", size="80%", pad=0.05)
sax = divider.append_axes("right", size="80%", pad=0.05)
fax = divider.append_axes("right", size="80%", pad=0.05)
ofax = divider.append_axes("right", size="100%", pad=0.05)
self.button_oneback = matplotlib.widgets.Button(playerax, label=u'$\u29CF$')
self.button_back = matplotlib.widgets.Button(bax, label=u'$\u25C0$')
self.button_stop = matplotlib.widgets.Button(sax, label=u'$\u25A0$')
self.button_forward = matplotlib.widgets.Button(fax, label=u'$\u25B6$')
self.button_oneforward = matplotlib.widgets.Button(ofax, label=u'$\u29D0$')
self.button_oneback.on_clicked(self.onebackward)
self.button_back.on_clicked(self.backward)
self.button_stop.on_clicked(self.stop)
self.button_forward.on_clicked(self.forward)
self.button_oneforward.on_clicked(self.oneforward)
def update_bar(i):
ax.cla()
ax.bar(labels,list(M[i,:]))
# Want y ticks to be labelled 0, .2, .4, .6, .8 for each frame.
ax.set_yticks(np.arange(0, 1, step=0.2))
ax.set_ylim(0, 1)
ax = fig.add_subplot(111)
ani = Player(fig, update_bar, maxi=num_times)
root.mainloop()
我一直在使用
我很难以 y 轴刻度标签按我希望的方式显示一系列条形图的动画。我的动画将有 100 帧,每帧由一个使用四个 x 值的条形图组成。数据存储在一个 100×4 的矩阵中,其条目是随机的并且介于 0 和 1 之间。动画的每一帧都出现在 FigureCanvas 上。下面是我到目前为止所做的总结,其中 Player
是上面 link 中定义的 class。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from tkinter import Tk, TOP, BOTH
root=Tk()
root.geometry('1000x1000')
fig=Figure()
# Place canvas on figure. Each frame of the animation will place a barplot on the
#canvas.
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=1)
# Random 100-by-4 matrix, each row of which corresponds to an instant in time.
M=np.random.rand(100,4)
labels=['a','b','c','d']
num_times=M.shape[0]
def update_bar(i):
ax=fig.add_subplot(111)
ax.bar(labels,list(M[i,:]))
# Want y ticks to be labelled 0, .2, .4, .6, .8 for each frame.
ax.set_yticks(np.arange(0, 1, step=0.2))
ani = Player(fig, update_bar, maxi=num_times)
root.mainloop()
当我播放动画时,y 轴标签变得一团糟,因为在每一帧中都重新创建了刻度线。 (参见显示第八帧的图像。)
有没有办法让我在动画开始之前一次性设置y-tickmark标签?
你应该移动这条线:
ax = fig.add_subplot(111)
out of update_bar
函数:您不需要在每次迭代中使用新的 ax
。
然后,在 update_bar
函数中你应该添加这一行:
ax.cla()
为了抹去之前的剧情
最后,我建议添加这一行:
ax.set_ylim(0, 1)
在update_bar
函数中,为了保持固定的y轴限制。
完整代码
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from tkinter import Tk, TOP, BOTH
import matplotlib
from matplotlib.animation import FuncAnimation
import mpl_toolkits.axes_grid1
root=Tk()
root.geometry('1000x1000')
fig=Figure()
# Place canvas on figure. Each frame of the animation will place a barplot on the
#canvas.
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=1)
# Random 100-by-4 matrix, each row of which corresponds to an instant in time.
M=np.random.rand(100,4)
labels=['a','b','c','d']
num_times=M.shape[0]
class Player(FuncAnimation):
def __init__(self, fig, func, frames=None, init_func=None, fargs=None,
save_count=None, mini=0, maxi=100, pos=(0.125, 0.92), **kwargs):
self.i = 0
self.min=mini
self.max=maxi
self.runs = True
self.forwards = True
self.fig = fig
self.func = func
self.setup(pos)
FuncAnimation.__init__(self,self.fig, self.func, frames=self.play(),
init_func=init_func, fargs=fargs,
save_count=save_count, **kwargs )
def play(self):
while self.runs:
self.i = self.i+self.forwards-(not self.forwards)
if self.i > self.min and self.i < self.max:
yield self.i
else:
self.stop()
yield self.i
def start(self):
self.runs=True
self.event_source.start()
def stop(self, event=None):
self.runs = False
self.event_source.stop()
def forward(self, event=None):
self.forwards = True
self.start()
def backward(self, event=None):
self.forwards = False
self.start()
def oneforward(self, event=None):
self.forwards = True
self.onestep()
def onebackward(self, event=None):
self.forwards = False
self.onestep()
def onestep(self):
if self.i > self.min and self.i < self.max:
self.i = self.i+self.forwards-(not self.forwards)
elif self.i == self.min and self.forwards:
self.i+=1
elif self.i == self.max and not self.forwards:
self.i-=1
self.func(self.i)
self.fig.canvas.draw_idle()
def setup(self, pos):
playerax = self.fig.add_axes([pos[0],pos[1], 0.22, 0.04])
divider = mpl_toolkits.axes_grid1.make_axes_locatable(playerax)
bax = divider.append_axes("right", size="80%", pad=0.05)
sax = divider.append_axes("right", size="80%", pad=0.05)
fax = divider.append_axes("right", size="80%", pad=0.05)
ofax = divider.append_axes("right", size="100%", pad=0.05)
self.button_oneback = matplotlib.widgets.Button(playerax, label=u'$\u29CF$')
self.button_back = matplotlib.widgets.Button(bax, label=u'$\u25C0$')
self.button_stop = matplotlib.widgets.Button(sax, label=u'$\u25A0$')
self.button_forward = matplotlib.widgets.Button(fax, label=u'$\u25B6$')
self.button_oneforward = matplotlib.widgets.Button(ofax, label=u'$\u29D0$')
self.button_oneback.on_clicked(self.onebackward)
self.button_back.on_clicked(self.backward)
self.button_stop.on_clicked(self.stop)
self.button_forward.on_clicked(self.forward)
self.button_oneforward.on_clicked(self.oneforward)
def update_bar(i):
ax.cla()
ax.bar(labels,list(M[i,:]))
# Want y ticks to be labelled 0, .2, .4, .6, .8 for each frame.
ax.set_yticks(np.arange(0, 1, step=0.2))
ax.set_ylim(0, 1)
ax = fig.add_subplot(111)
ani = Player(fig, update_bar, maxi=num_times)
root.mainloop()