使用matplotlib进行实时更新 [英] live updating with matplotlib

查看:2021
本文介绍了使用matplotlib进行实时更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一些手机加速度计数据,我想基本制作一个关于手机运动的视频。因此,我使用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()
返回数据
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标签')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()

现在时间很重要,实际上也是我一次只能看到一个点的因素,因为时间也是一个因素,它让我看着程序

我能做些什么来使它成为实时更新图表?



只有我能想到的是有一个循环遍历并逐行生成图形,但是这会打开很多文件,因为我有数百万行,所以它会被疯狂。



那么我该如何创建一个实时更新图表?

解决方案

这里是一个裸机例子,它可以尽快更新:

  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]

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

诀窍是 not 继续创建新图形,因为这将继续消耗内存,但要更改现有plo上的x,y,z数据吨。使用 .ion() .draw()设置画布进行更新。



附录:@Kelsey以下排名很高的评论指出:


您可能需要在 plt.draw()行之后添加一个 plt.pause(0.01)显示



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:

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?

解决方案

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 not to 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.

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

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

这篇关于使用matplotlib进行实时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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