如何在Python中从plot设置圆的动画?

2024-09-26 18:12:41 发布

您现在位置:Python中文网/ 问答频道 /正文

对于生成的一组点,在每个点上绘制圆,其中一些圆与多边形相交,如下所示

  1. 我的意图是创造一个动画,显示每个情节一个接一个的圆圈。我该怎么做(例如:首先,只显示绿色圆圈,然后显示橙色,以此类推)
  2. 对于动画中显示的每个圆,是否也可以显示圆和多边形之间的交点? Circle and Polygon

生成圆和分布的代码:

from descartes import PolygonPatch
import matplotlib.pyplot as plt
import alphashape
import numpy as np
import matplotlib as mpl
from matplotlib.collections import PatchCollection

xx = np.linspace(40,50,5)
yy = np.linspace(90,110,5)
zz = np.vstack((xx,yy)).T
fig = plt.figure(1, figsize=(12, 5))
ax = fig.add_subplot(111, aspect='equal')
x = np.random.uniform(45,60, 1000)
y = np.random.uniform(100, 110, 1000)
test = np.vstack((x,y)).T

alpha_shape = alphashape.alphashape(test, 0.)
plt.scatter(*zip(*test))
ax.add_patch(PolygonPatch(alpha_shape, alpha=0.2))

colormap = mpl.cm.Dark2.colors 
plt.plot(xx,yy,zorder=3)
plt.scatter(xx,yy,zorder=2)
circles = []
for i, colori in zip(range(0,len(xx)), colormap):
    circles.append(plt.Circle(zz[i], 3, color=colori, fill=False))
coll = PatchCollection(circles, match_original=True, zorder=1)
ax.add_collection(coll)
plt.grid(b=None)
plt.autoscale()
plt.show()

Tags: testimportalphaaddmatplotlibasnpplt

热门问题