(Python)Matplotlib动画不显示 [英] (python) matplotlib animation doesn't show
问题描述
我基于 matplotlib
网站
我在哪里出错了?
问题是你首先使用单色(1.0)定义图,因此将颜色范围设置为此。更新图形时,颜色范围是1.0 +-很小的值,因此您看不到更改。您需要使用 vmin
/ vmax
参数将颜色范围设置为一到零,如下所示:
im = plt.imshow(视口,动画= True,vmin = 0。,vmax = 1。)
其余的代码保持不变,这应该可以正常工作。另一种选择是添加呼叫,
im.autoscale()
在
im.set_array(viewpoint)
之后的pre>
强制每次更新颜色范围。
I wrote the following code based on the
matplotlib
site example.import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation fig = plt.figure() nFreqs = 1024 nFFTWindows = 512 viewport = np.ones((nFreqs, nFFTWindows)) im = plt.imshow(viewport, animated=True) def updatefig(*args): global viewport print viewport viewport = np.roll(viewport, -1, axis=1) viewport[:, -1] = 0 im.set_array(viewport) return im, ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True) plt.show()
Before changing the animation works, but now it doesn't. I expected it to start with a purple plot, which slowly turns yellow from the right edge to the left. The
viewport
variable does update correctly (checked it withI get the static image (all ones, like it was initially):
Where did I go wrong here?
解决方案The problem is you are defining a plot initially with a single colour (1.0) so the colour range is set to this. When you update the figure, the range of colours is 1.0 +- some small value so you don't see the change. You need to set the colour range to between one and zero with
vmin
/vmax
arguments as follows:im = plt.imshow(viewport, animated=True, vmin=0., vmax=1.)
The rest of the code stays the same and this should work as expected. Another alternative is to add the call,
im.autoscale()
after
im.set_array(viewpoint)
to force the colour range to be updated each time.这篇关于(Python)Matplotlib动画不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!