如何使用matplotlib实时绘制更新的numpy ndarray? [英] How do I plot an updating numpy ndarray in real time using matplotlib?
问题描述
我有一个使用 np.zeros 在循环外初始化的 numpy 数组.使用for循环内的某些函数更新此数组.我希望绘制数组,因为它随着每次迭代而变化.
我在这里看到的大多数答案都是针对列表的,而不是针对ndarrays的.我看过以下链接.我曾尝试为其中的一些目的进行修改,但无济于事.
https://github.com/stsievert/python-drawnow/blob/master/drawnow/drawnow.py @Scott Sievert,我也看到了你的代码.但遗憾的是,我一直无法弄清楚如何修改它.
在 Python 中使用 matplotlib 和 kivy 实时绘图
在 Python 中使用 matplotlib 和 kivy 实时绘图
https://realpython.com/python-matplotlib-guide/>
https://gist.github.com/vaclavcadek/66c9c61a1fac30150514a665c4bcb5dc
http://jakevdp.github.io/博客/2012/08/18/matplotlib-animation-tutorial/
所以基本上我想实时查看ndarray y的值.(请参见下面的代码)
我正在将其作为脚本运行.@ Scott Staniewicz
from numpy.random import random_sample从 numpy 导入范围,零x =范围(0,10)y = 零((10, 1))对于范围内的我(10):y[i] = sin(random_sample())
最基本的版本应该是
将 numpy 导入为 np导入matplotlib.pyplot作为pltx = np.arange(0, 10)y = np.zeros((10, 1))无花果= plt.figure()线,= plt.plot(x,y)ylim(-0.2,1.2)对于范围内的我(10):y[i] = np.sin(np.random.random_sample())line.set_data(x[:i+1], y[:i+1])请暂停(1)plt.show()
I have a numpy array which I initialized outside the loop using np.zeros. This array is updated using some function inside a for a loop. I wish to plot the array as it changes with each iteration.
Most of the answers I have seen here are for lists and not ndarrays. I have seen the following links. Some of them I have tried to modify for my purpose but to no avail.
How to update a plot in matplotlib?
https://github.com/stsievert/python-drawnow/blob/master/drawnow/drawnow.py @Scott Sievert, I saw your code too. But unfortunately, I haven't been able to figure out how to modify it.
Real-time plotting using matplotlib and kivy in Python
Real-time plotting using matplotlib and kivy in Python
Real time trajectory plotting - Matplotlib
https://realpython.com/python-matplotlib-guide/
https://gist.github.com/vaclavcadek/66c9c61a1fac30150514a665c4bcb5dc
http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/
So basically I want to see the value of the ndarray y in real-time. (see the code below)
I am running it as a script.@Scott Staniewicz
from numpy.random import random_sample
from numpy import arange, zeros
x = arange(0, 10)
y = zeros((10, 1))
for i in range(10):
y[i] = sin(random_sample())
The most basic version would look like
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 10)
y = np.zeros((10, 1))
fig = plt.figure()
line, = plt.plot(x,y)
plt.ylim(-0.2,1.2)
for i in range(10):
y[i] = np.sin(np.random.random_sample())
line.set_data(x[:i+1], y[:i+1])
plt.pause(1)
plt.show()
这篇关于如何使用matplotlib实时绘制更新的numpy ndarray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!