【发布时间】:2026-01-06 19:20:05
【问题描述】:
我一直在尝试根据图表tripcolor 在matplotlib 中创建动画。假设我有
field = ax.tripcolor(tri, C)
如何在每次动画迭代后改变 C 的值?
非常感谢,
【问题讨论】:
标签: python animation matplotlib
我一直在尝试根据图表tripcolor 在matplotlib 中创建动画。假设我有
field = ax.tripcolor(tri, C)
如何在每次动画迭代后改变 C 的值?
非常感谢,
【问题讨论】:
标签: python animation matplotlib
field 保证是 matplotlib.collections.Collection 基类的实例,它有助于为此类情况定义 set_array() 方法。
在动画的每次迭代中,只需将 C 的新值传递给 field.set_array() 方法。假设您将 FuncAnimation 类用于动画,正如您可能想要的那样,这简化为:
fig = plt.figure()
ax = plt.subplot(111)
field = ax.tripcolor(tri, C)
def update_tripcolor(frame_number):
# Do something here to update "C"!
C **= frame_number # ...just not this.
# Update the face colors of the previously plotted triangle mesh.
field.set_array(C)
# To triangular infinity and beyond! (Wherever that is. It's probably scary.)
FuncAnimation(fig, update_tripcolor, frames=10)
另一方面,更新tri 要困难得多。虽然这个问题并没有尝试这样做,但有洞察力的读者可能会好奇地知道,您基本上必须将整个三角形网格(即field)删除、重新创建并重新添加到该图的轴上。当然,这既低效又痛苦。 (欢迎使用 Matplotlib。人口:你。)
愿field.set_array() 与您同在。
【讨论】: