【问题标题】:How do I plot an updating numpy ndarray in real time using matplotlib?如何使用 matplotlib 实时绘制更新的 numpy ndarray?
【发布时间】:2019-12-09 17:33:18
【问题描述】:

我有一个 numpy 数组,我在循环外使用 np.zeros 对其进行了初始化。 该数组使用 for 循环中的某个函数进行更新。我希望绘制随着每次迭代而变化的数组。

我在这里看到的大多数答案都是针对列表而不是 ndarray。 我看过以下链接。其中一些我已经尝试修改以达到我的目的,但无济于事。

How to update a plot in matplotlib?

https://github.com/stsievert/python-drawnow/blob/master/drawnow/drawnow.py @Scott Sievert,我也看到了你的代码。但不幸的是,我一直无法弄清楚如何修改它。

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/

Matplotlib: Animate 2D Array

所以基本上我想实时查看 ndarray y 的值。 (见下面的代码)

我将它作为脚本运行。@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())

【问题讨论】:

  • 您是否有任何代码示例几乎可以工作,但不完全?我已经看到了几种绘制更新 ndarray 图像的方法,有些更复杂,但更难工作,所以看看你要做什么会很有帮助
  • 另外,了解您如何尝试运行此代码也会很有帮助:例如作为脚本,在 iPython 终端中,在 jupyter 笔记本中......其中一些情况比其他情况更容易工作
  • @ScottStaniewicz 我已经在编辑中添加了信息

标签: python numpy matplotlib numpy-ndarray


【解决方案1】:

免责声明:我很确定我的答案不是最理想的,但这是我现在可以实现的。

修改@Scott (Scott Sievert) answer 并使用他的drawnow Github package 我已经整理了这个答案。我没有安装 drawnow Github package 。相反,我只是将 drawow.py 复制到我的文件夹中。 (这是因为我没有找到任何通过 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)

【讨论】:

    【解决方案2】:

    最基本的版本看起来像

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

    【讨论】:

    • 不错,短而甜美。如果重复使用,只需要记住清除轴(使用 plt.cla()),关闭图形(使用 plt.close(fig))。
    猜你喜欢
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2023-02-09
    • 2021-07-24
    相关资源
    最近更新 更多