简体   繁体   English

如何使用matplotlib实时绘制更新的numpy ndarray?

[英]How do I plot an updating numpy ndarray in real time using matplotlib?

I have a numpy array which I initialized outside the loop using np.zeros. 我有一个numpy数组,我使用np.zeros在循环外部进行了初始化。 This array is updated using some function inside a for a loop. 使用for循环内的某些函数更新此数组。 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. 我在这里看到的大多数答案都是针对列表的,而不是针对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? 如何在matplotlib中更新图?

https://github.com/stsievert/python-drawnow/blob/master/drawnow/drawnow.py @Scott Sievert, I saw your code too. https://github.com/stsievert/python-drawnow/blob/master/drawnow/drawnow.py @Scott Sievert,我也看到了您的代码。 But unfortunately, I haven't been able to figure out how to modify it. 但不幸的是,我无法弄清楚如何对其进行修改。

Real-time plotting using matplotlib and kivy in Python 在Python中使用Matplotlib和Kivy进行实时绘图

Real-time plotting using matplotlib and kivy in Python 在Python中使用Matplotlib和Kivy进行实时绘图

Real time trajectory plotting - Matplotlib 实时轨迹图-Matplotlib

https://realpython.com/python-matplotlib-guide/ https://realpython.com/python-matplotlib-guide/

https://gist.github.com/vaclavcadek/66c9c61a1fac30150514a665c4bcb5dc https://gist.github.com/vaclavcadek/66c9c61a1fac30150514a665c4bcb5dc

http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

Matplotlib: Animate 2D Array Matplotlib:动画二维数组

So basically I want to see the value of the ndarray y in real-time. 所以基本上我想实时查看ndarray y的值。 (see the code below) (请参见下面的代码)

I am running it as a script.@Scott Staniewicz 我将其作为脚本运行。@ 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())

Disclaimer : I am quite sure my answer is not the most optimal, but this is what I could achieve for now. 免责声明:我很确定我的答案不是最佳的,但这是我现在可以实现的。

Modifying @Scott (Scott Sievert) answer and using his drawnow Github package I have put together this answer. 修改@Scott(Scott Sievert) 答案,并使用他的drawow Github软件包,我将这个答案汇总在一起。 I didn't install drawnow Github package . 我没有安装drawow Github软件包 Instead I just copied drawnow.py into my folder. 相反,我只是将drawdow.py复制到了我的文件夹中。 (This is because I didn't find any way to install it via conda. I didn't wan't to use PyPi) (这是因为我没有找到通过conda安装它的任何方法。我不想使用PyPi)

from numpy.random import random_sample
from numpy import arange, zeros
from math import sin
from drawnow import drawnow
from matplotlib import use
from matplotlib.pyplot import figure, axes, ion
from matplotlib import rcParams
from matplotlib.pyplot import style
from matplotlib.pyplot import cla, close
use("TkAgg")
pgf_with_rc_fonts = {"pgf.texsystem": "pdflatex"}
rcParams.update(pgf_with_rc_fonts)
style.use('seaborn-whitegrid')

max_iter = 10**(3)  # 10**(5)  # 10**(2)
y = zeros((max_iter, 1))


def draw_fig():
    # can be arbitrarily complex; just to draw a figure
    # figure() # don't call!
    scott_ax = axes()
    scott_ax.plot(x, y, '-g', label='label')
    # cla()
    # close(scott_fig)
    # show() # don't call!


scott_fig = figure()  # call here instead!
ion()
# figure()  # call here instead!
# ion()    # enable interactivity
x = arange(0, max_iter)
for i in range(max_iter):
    # cla()
    # close(scott_fig)
    y[i] = sin(random_sample())
    drawnow(draw_fig)

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM