在Matplotlib动画中更新Tripcolor图形

在Matplotlib动画中更新Tripcolor图形

本文介绍了在Matplotlib动画中更新Tripcolor图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图从 tripcolor 图中的 matplotlib 中创建动画。假设我有

I have been trying to create an animation in matplotlib from the graph tripcolor. Let's say I have

field = ax.tripcolor(tri, C)

每次动画迭代后如何更改C的值?

How do I change the value of C after each iteration of the animation?

非常感谢,

推荐答案

字段保证是<$ c的实例$ c> matplotlib.collections.Collection 基类,在此类情况下,它有助于定义 set_array()方法。

field is guaranteed to be an instance of the matplotlib.collections.Collection base class, which helpfully defines a set_array() method for just such occasions.

在动画的每次迭代中,只需将新值 C 传递给 field.set_array()方法。假设您可能希望使用 FuncAnimation 类来制作动画,这可以简化为:

In each iteration of your animation, simply pass the new value of C to the field.set_array() method. Assuming you use the FuncAnimation class for animations, as you probably want to, this reduces to:

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。人口:您。

Updating tri, on the other hand, is considerably more difficult. While this question doesn't attempt to do so, the perspicacious reader may be curious to learn that you basically have to remove, recreate, and re-add the entire triangle mesh (i.e., field) onto this figure's axes. This is both inefficient and painful, of course. (Welcome to Matplotlib. Population: you.)

可以在 field.set_array ()与您同在。

这篇关于在Matplotlib动画中更新Tripcolor图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:42