Matplotlib动画多边形

编程入门 行业动态 更新时间:2024-10-26 18:16:13
本文介绍了Matplotlib动画多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用matplotlib,试图将animate函数与多边形一起使用,以使其在平面中旋转。

Using matplotlib, I am trying to use the animate function with a polygon, to rotate it in the plane.

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

The code pasted below only draws the first polygon but the animation doesn't work.

有人可以发现 animate 函数的问题吗?

Can someone spot what's wrong in the animate function?

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()

推荐答案

行:

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

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

does not update the vertices of the polygon. You can use set_xy to change the vertices:

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

这将使每个图的变化迭代。 Matplotlib艺术家文档具有更多信息。

This will make the plot change on each iteration. The Matplotlib artists documentation has more information.

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

It still doesn't look like the shape is rotating. That's something to do with the coordinates you're using.

更多推荐

Matplotlib动画多边形

本文发布于:2023-08-02 00:07:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1272914.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多边形   动画   Matplotlib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!