Python matplotlib:条形图动画只工作一次
我使用Python matplotlib:条形图动画只工作一次,python,matplotlib,animation,jupyter-notebook,Python,Matplotlib,Animation,Jupyter Notebook,我使用matplotlib.animation的FuncAnimation方法实现了一个动画 代码中没有错误,但我不知道问题到底出在哪里 代码: def visualization(self): fig = plt.figure() def animation_frame(i): print(i) trimmed_dist = self.get_distributions(i, self.window_size + i)
matplotlib.animation
的FuncAnimation
方法实现了一个动画
代码中没有错误,但我不知道问题到底出在哪里
代码:
def visualization(self):
fig = plt.figure()
def animation_frame(i):
print(i)
trimmed_dist = self.get_distributions(i, self.window_size + i)
# create labels
label_no = len(trimmed_dist)
labels = []
for index in range(label_no):
from_ = index * self.bucket_length
to_ = (index + 1) * self.bucket_length
label = '{:.2f} - {:.2f}'.format(from_, to_)
labels.append(label)
#create bar chart
colors = plt.cm.Dark2(range(label_no))
plt.xticks(rotation=90)
plt.bar(x=labels, height=trimmed_dist, color=colors)
frames_no = len(self.percentages) - self.window_size
print('frames_no:', frames_no)
animation = FuncAnimation(fig, animation_frame, frames=frames_no, interval=1000)
return animation
输出:
def visualization(self):
fig = plt.figure()
def animation_frame(i):
print(i)
trimmed_dist = self.get_distributions(i, self.window_size + i)
# create labels
label_no = len(trimmed_dist)
labels = []
for index in range(label_no):
from_ = index * self.bucket_length
to_ = (index + 1) * self.bucket_length
label = '{:.2f} - {:.2f}'.format(from_, to_)
labels.append(label)
#create bar chart
colors = plt.cm.Dark2(range(label_no))
plt.xticks(rotation=90)
plt.bar(x=labels, height=trimmed_dist, color=colors)
frames_no = len(self.percentages) - self.window_size
print('frames_no:', frames_no)
animation = FuncAnimation(fig, animation_frame, frames=frames_no, interval=1000)
return animation
PS 1:帧号的值为877
PS 2:我认为问题在于可视化方法中的返回。因此,我更改了代码,但仍然无法正常工作。我相信您正在Jupyter笔记本中运行代码。在这种情况下,您应该在代码开头添加%matplotlib notebook
。
作为参考,请尝试运行您可以在中找到的代码,以查看它是否为您运行
编辑
我在笔记本上实现了你的一部分代码。由于我不知道什么是self.percentage
,self.window\u size
,self.get\u distributions
和self.bucket\u length
以及它们有哪些值,我设置了标签=['a',b',c']
和trimmed\u dist=[3*I,2*I,I**2]
为了简单起见,为了运行一个简单的动画。
这是我的代码:
%matplotlib notebook
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
def animation_frame(i):
plt.gca().cla()
labels = ['a', 'b', 'c']
trimmed_dist = [3*i, 2*i, i**2]
label_no = len(trimmed_dist)
colors = plt.cm.Dark2(range(label_no))
plt.xticks(rotation=90)
plt.bar(x=labels, height=trimmed_dist, color=colors)
plt.title(f'i = {i}') # this replaces print(i)
plt.ylim([0, 100]) # only for clarity purpose
fig = plt.figure()
frames_no = 877
print('frames_no:', frames_no)
animation = FuncAnimation(fig, animation_frame, frames=frames_no, interval=1000)
这就是结果
我添加了plt.gca().cla()
,以便在每次迭代中删除前一帧。
print(i)
语句对我也不起作用,所以我用plt.title(f'i={i}')
替换它,以便在标题中写入i
。相反,print('frames\u no:',frames\u no)
工作正常
如您所见,我的动画正在运行,因此请尝试实现我对代码所做的更改。
如果动画仍然没有运行,请尝试检查self.percentage
,self.window\u size
,self.get\u distributions
和self.bucket\u length
值和类型,以确保标签和修剪距离的计算正确。添加plt.show()
在返回声明生效之前?为了更好地帮助您,请您提供一个?