尝试执行此代码:
"""
A simple example of an animated plot
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))
def animate(i):
line.set_ydata(np.sin(x + i/10.0)) # update the data
return line,
# Init only required for blitting to give a clean slate.
def init():
line.set_ydata(np.ma.array(x, mask=True))
return line,
ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,
interval=25, blit=True)
plt.show()
Run Code Online (Sandbox Code Playgroud)
源代码:https : //matplotlib.org/examples/animation/simple_anim.html
在 PyCharm 中显示: …
我是python的新手,只是安装了pyCharm并尝试运行给出以下问题的测试示例:如何在matplotlib中更新绘图?
本示例更新该图以动画化正弦信号。代替重新绘制,它更新了绘图对象的数据。它可以在命令行中运行,但是在PyCharm中运行时该图不会显示。plt.show(block=True)在脚本的末尾添加会弹出该图,但是这次不会更新。
有任何想法吗?
我正在尝试使用该包matplotlib.animation在 PyCharm 中绘制动画。但是,PyCharm 仅在 PNG 图形中显示动画的第一帧。
动画是关于一个移动的矩形,python版本是2.7.14,代码在这里:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib import animation
x = [0, 1, 2]
y = [0, 1, 2]
yaw = [0.0, 0.5, 1.3]
fig = plt.figure()
plt.axis('equal')
plt.grid()
ax = fig.add_subplot(111)
ax.set_xlim(-10, 10)
ax.set_ylim(-10, 10)
patch = patches.Rectangle((0, 0), 0, 0, fc='y')
def init():
ax.add_patch(patch)
return patch,
def animate(i):
patch.set_width(1.2)
patch.set_height(1.0)
patch.set_xy([x[i], y[i]])
patch._angle = -np.rad2deg(yaw[i])
return patch,
anim = animation.FuncAnimation(fig, animate,
init_func=init, …Run Code Online (Sandbox Code Playgroud)