【问题标题】:Matplotlib Animation PolygonsMatplotlib 动画多边形
【发布时间】:2014-03-26 09:45:14
【问题描述】:

使用matplotlib,我正在尝试使用带有多边形的动画函数,在平面上旋转它。

下面粘贴的代码只绘制了第一个多边形,但动画不起作用。

有人能发现animate 函数有什么问题吗?

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from matplotlib.patches import Polygon
fig = plt.figure()
fig.set_dpi(100)
fig.set_size_inches(7, 6.5)

ax = plt.axes(xlim=(-30, 30), ylim=(-30, 30))


pts = [[0.,0.], [2.,0.], [9.6,7.6], [9.6,9.6]]


patch = plt.Polygon(pts,closed=None, fill=None, edgecolor='r')
def init():
    patch.pts = [[0.,0.], [2.,0.], [9.6,7.6], [9.6,9.6]]
    ax.add_patch(patch)
    return patch,

def animate(i):

    x1=0.+ 10. * np.sin(np.radians(i))
    x2=2.+ 10. * np.sin(np.radians(i))
    x3=9.6+ 10. * np.sin(np.radians(i))
    x4=9.6+ 10. * np.sin(np.radians(i))

    y1=0.+ 10. * np.cos(np.radians(i))
    y2=0.+ 10. * np.cos(np.radians(i))
    y3=7.6+ 10. * np.cos(np.radians(i))
    y4=9.6+ 10. * np.cos(np.radians(i))


    patch.pts = [[x1,y1], [x2,y2], [x3,y3], [x4,y4]]


    return patch,


anim = animation.FuncAnimation(fig,animate,init_func=init,frames=36,interval=1000,blit=True)


plt.show()

【问题讨论】:

    标签: python animation matplotlib


    【解决方案1】:

    行:

    patch.pts = [[x1,y1], [x2,y2], [x3,y3], [x4,y4]]
    

    不更新多边形的顶点。您可以使用 set_xy 更改顶点:

    patch.set_xy([[x1,y1], [x2,y2], [x3,y3], [x4,y4]])
    

    这将使情节在每次迭代时发生变化。 Matplotlib artists documentation 有更多信息。

    形状看起来仍然没有旋转。这与您使用的坐标有关。

    【讨论】:

      猜你喜欢
      • 2016-11-15
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      • 2021-03-30
      • 2015-08-11
      • 1970-01-01
      相关资源
      最近更新 更多