我怎样才能让 python 图的点随着时间的推移出现? [英] How can i make points of a python plot appear over time?

查看:36
本文介绍了我怎样才能让 python 图的点随着时间的推移出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个动画,其中我的数据点会逐渐出现在我的图表上,并在所有数据点都出现时冻结.我已经看过相关性了,但我不太确定如何只用单个点自己做

这不是什么特别有用的东西,但我认为它看起来很酷,因为我试图在地图上可视化一些位置数据

我知道这不是很清楚,所以请澄清一下,我不太确定如何很好地表达我的问题.

谢谢

解决方案

要使动画在 matplotlib 窗口中完成时看起来停止,您需要使其无限(省略 FuncAnimation 中的 frames 参数),并将帧计数器设置为帧系列中的最后一个数字:

def animate(i):如果我>9:我 = 9graph.set_data(x [:i + 1],y [:i + 1])返回图ani = FuncAnimation(fig, animate, interval=200)

或者,根据对 这个 问题.

要为散点图设置动画,您需要使用其他方法.一段代码值一千字:

将 numpy 导入为 np导入matplotlib.pyplot作为plt从matplotlib.animation导入FuncAnimationx = np.arange(10)y = np.random.random(10)大小= np.random.randint(150,大小= 10)颜色 = np.random.choice(["r", "g", "b"], size=10)无花果= plt.figure()plt.xlim(0,10)plt.ylim(0, 1)图 = plt.scatter([], [])定义动画(i):graph.set_offsets(np.vstack((x [:i + 1],y [:i + 1])).T)graph.set_sizes(size[:i+1])graph.set_facecolors(colors[:i+1])返回图ani = FuncAnimation(fig, animate, repeat=False, interval=200)plt.show()

I would like to create a animation where my data points would gradually appear on my graph and freeze when all the data points have appeared. I've seen in done with correlations i'm just not too sure how to do it with just individual points themselves

This isn't something that will show anything particularly useful but i though it would look cool since i am trying to visualize some location data on a map

I know this isn't very clear so please as for clarifications, I'm not too sure how to phrase my problem very well.

Thanks

解决方案

matplotlib.animation.FuncAnimation is the right tool for you. First create an empty graph, and then gradually add data points to it in the function. The following piece of code will illustrate it:

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

x = np.arange(10)
y = np.random.random(10)

fig = plt.figure()
plt.xlim(0, 10)
plt.ylim(0, 1)
graph, = plt.plot([], [], 'o')

def animate(i):
    graph.set_data(x[:i+1], y[:i+1])
    return graph

ani = FuncAnimation(fig, animate, frames=10, interval=200)
plt.show()

The result (saved as gif file) is shown below:

EDIT: To make the animation look stopped when finished in matplotlib window, you need to make it infinite (omit frames parameter in FuncAnimation), and set the frame counter to the last number in your frame series:

def animate(i):
    if i > 9:
        i = 9
    graph.set_data(x[:i+1], y[:i+1])
    return graph

ani = FuncAnimation(fig, animate, interval=200)

Or, which is better, you can set repeat parameter in FuncAnimation to False, as per answer to this question.

EDIT 2: To animate a scatter plot, you need a whole bunch of other methods. A piece of code is worth a thousand words:

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

x = np.arange(10)
y = np.random.random(10)
size = np.random.randint(150, size=10)
colors = np.random.choice(["r", "g", "b"], size=10)

fig = plt.figure()
plt.xlim(0, 10)
plt.ylim(0, 1)
graph = plt.scatter([], [])

def animate(i):
    graph.set_offsets(np.vstack((x[:i+1], y[:i+1])).T)
    graph.set_sizes(size[:i+1])
    graph.set_facecolors(colors[:i+1])
    return graph

ani = FuncAnimation(fig, animate, repeat=False, interval=200)
plt.show()

这篇关于我怎样才能让 python 图的点随着时间的推移出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆