繁体   English   中英

如何更新 matplotlib 中的 PolyCollection

[英]How to update PolyCollection in matplotlib

我想知道,是否有办法重绘已经创建的 PolyCollection。

MWP

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0.0, 2, 0.01)
y = np.sin(2*np.pi*x)

fig, (ax,ax2) = plt.subplots(1,1)

polycolelction = ax.fill_between(x, y)

for i in range(10):
    y = 1.2*np.sin(i*np.pi*x)

    # here should be data update, probably something like set_offset or set_verts?
    polycolelction.set_verts([x,y])
    fig.canvas.draw()
    fig.canvas.flush_events()

从头开始创建多边形然后更新其顶点可能更容易。 这样,我们就可以精确控制顶点的数量以及多边形的表示方式。

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection

x = np.arange(0.0, 2, 0.01)
y = np.sin(2 * np.pi * x)

fig, ax = plt.subplots()

points = np.array([x, y]).T
points = np.vstack([points, [points[-1, 0], 0], [points[0, 0], 0]])
polycollection = PatchCollection([Polygon(points, closed=True)])
pathcollection = ax.add_collection(polycollection)
ax.set_xlim(x[0], x[-1])
ax.set_ylim(-1.3, 1.3)

for i in range(10):
    y = 1.2 * np.sin(i * np.pi * x)
    points = np.array([x, y]).T
    points = np.vstack([points, [points[-1, 0], 0], [points[0, 0], 0]])
    pathcollection.get_paths()[0].vertices = points
    fig.canvas.draw()
    fig.canvas.flush_events()

fill_between 动画

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM