python的plot如何实时更新,使用matplotlib进行实时更新

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值