Python 使用 matplotlib 实时更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16446443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:38:14  来源:igfitidea点击:

live updating with matplotlib

pythongraphmatplotlib

提问by Ryan Saxe

So I have some phone accelerometry data and I would like to basically make a video of what the motion of the phone looked like. So I used matplotlib to create a 3D graph of the data:

所以我有一些手机加速度测量数据,我想基本上制作一个关于手机运动的视频。所以我使用 matplotlib 来创建数据的 3D 图:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pandas as pd
import pickle
def pickleLoad(pickleFile):
    pkl_file = open(pickleFile, 'rb')
    data = pickle.load(pkl_file)
    pkl_file.close()
    return data
data = pickleLoad('/Users/ryansaxe/Desktop/kaggle_parkinsons/accelerometry/LILY_dataframe')
data = data.reset_index(drop=True)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
xs = data['x.mean']
ys = data['y.mean']
zs = data['z.mean']
ax.scatter(xs, ys, zs)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()

Now time is important and is actually also a factor that I only see one point at a time because time is also a factor and it lets me watch the progression of the accelerometry data!

现在时间很重要,实际上也是一个因素,我一次只能看到一个点,因为时间也是一个因素,它让我观察加速度数据的进展!

What can I do with this to make it a live updating graph?

我该怎么做才能使它成为实时更新图?

Only thing I can think of is to have a loop that goes through row by row and makes the graph from the row, but that will open so many files that it would be insane because I have millions of rows.

我唯一能想到的是有一个循环,逐行遍历并从行中生成图形,但这将打开太多文件,这将是疯狂的,因为我有数百万行。

So how can I create a live updating graph?

那么如何创建实时更新图呢?

回答by Hooked

Here is a bare-bones example that updates as fast as it can:

这是一个尽可能快地更新的基本示例:

import pylab as plt
import numpy as np

X = np.linspace(0,2,1000)
Y = X**2 + np.random.random(X.shape)

plt.ion()
graph = plt.plot(X,Y)[0]

while True:
    Y = X**2 + np.random.random(X.shape)
    graph.set_ydata(Y)
    plt.draw()

The trick is notto keep creating new graphs as this will continue to eat up memory, but to change the x,y,z-data on an existing plot. Use .ion()and .draw()setup the canvas for updating like this.

诀窍不是继续创建新图,因为这会继续占用内存,而是更改现有图上的 x、y、z 数据。使用.ion().draw()设置画布进行更新。

Addendum: A highly ranked comment below by @Kelsey notes that:

附录:@Kelsey 在下面的评论中指出:

You may need a plt.pause(0.01)after the plt.draw()line to get the refresh to show

您可能需要在行plt.pause(0.01)之后plt.draw()才能显示刷新

回答by Ryan Saxe

I was able to create live updating with draw()and a while loop here is the code I used:

我能够创建实时更新,draw()这里的 while 循环是我使用的代码:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from pylab import *
import time
import pandas as pd
import pickle
def pickleLoad(pickleFile):
    pkl_file = open(pickleFile, 'rb')
    data = pickle.load(pkl_file)
    pkl_file.close()
    return data
data = pickleLoad('/Users/ryansaxe/Desktop/kaggle_parkinsons/accelerometry/LILY_dataframe')
data = data.reset_index(drop=True)
df = data.ix[0:,['x.mean','y.mean','z.mean','time']]
ion()
fig = figure()
ax = fig.add_subplot(111, projection='3d')
count = 0
plotting = True
while plotting:
    df2 = df.ix[count]
    count += 1
    xs = df2['x.mean']
    ys = df2['y.mean']
    zs = df2['z.mean']
    t = df2['time']
    ax.scatter(xs, ys, zs)
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    ax.set_title(t)
    draw()
    pause(0.01)
    if count > 50:
        plotting = False
ioff()
show()