简体   繁体   English

如何使用matplotlib为我的图形制作动画,以便看起来数据点在移动?

[英]How can I animate my graph with matplotlib so that it looks like the data points are moving?

I have two 2D arrays and I want to display the data in a scatter graph so it will look like the points are moving. 我有两个2D数组,我想在散点图中显示数据,以便看起来像点在移动。 So I want the first set of x and y data to be plotted then disappear to be replaced by the next set of x and y data etc. 所以我想绘制第一组x和y数据,然后消失以替换为下一组x和y数据,等等。

the code I have currently just plots all the data points and joins them up, effectively tracing out the paths of the data points. 我目前使用的代码只是绘制所有数据点并将它们连接起来,从而有效地找出数据点的路径。

pyplot.figure()
    for i in range(0,N):
        pyplot.plot(x[i,:],y[i,:],'r-')
pyplot.xlabel('x /m')
pyplot.ylabel('y /m')
pyplot.show()

any help is much appreciated. 任何帮助深表感谢。

The matplotlib docs contain some animation examples that may be useful. matplotlib文档包含一些可能有用的动画示例 They all use the matplotlib.animation API, so I'd suggest you read through that for some ideas. 他们都使用matplotlib.animation API,因此建议您通读一下以获取一些想法。 From the examples, here's a simple animated sine curve using FuncAnimation : 从示例中,这是一个使用FuncAnimation的简单动画正弦曲线:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)        # x-array
line, = ax.plot(x, np.sin(x))

def animate(i):
    line.set_ydata(np.sin(x+i/10.0))  # update the data
    return line,

#Init only required for blitting to give a clean slate.
def init():
    line.set_ydata(np.ma.array(x, mask=True))
    return line,

ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,
    interval=25, blit=True)
plt.show()

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

相关问题 我如何告诉python我的数据结构(二进制)是什么样子,以便我可以绘制它? - How do I tell python what my data structure (that is in binary) looks like so I can plot it? 如何使用matplotlib为一组点设置动画? - How can I animate a set of points with matplotlib? 我有一个值数组,如何使用 matplotlib 的 plot 值使其看起来像 25 秒的视频 - I have an array of values, how can I plot values using matplotlib so that it looks like a video of 25 seconds 我的 matplotlib 图上绘制了多少数据点? - How many data points are plotted on my matplotlib graph? 我如何修改此代码,以免每次都重新绘制图形,matplotlib - How can I modify this code so that I am not replotting my graph everytime, matplotlib 如何使用 Matplotlib 在可读图形中格式化数千个日期相关数据点? - How can I format multiple thousands of date-dependant data points in a readable graph with Matplotlib? 如何在 Matplotlib 中绘制没有线和点的误差条图? - How can I draw an errorbar graph without lines and points in Matplotlib? 我如何在 matplotlib 中获得一个动画图形? - How do i get a graph to animate in matplotlib? 如何在 matplotlib 图上设置动画 - How to animate on matplotlib graph 如何在Matplotlib图形中标记特定数据点 - How to mark specific data points in matplotlib graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM