使用 MatPlotLib 绘制连续的数据流 [英] Plotting a continuous stream of data with MatPlotLib

查看:98
本文介绍了使用 MatPlotLib 绘制连续的数据流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用MatPlotLib绘制图形,该图形随时间变化.在每个时间步长处,都会将一个附加数据点添加到绘图中.但是,只能显示一个图形,其外观会随着时间而变化.

I want to use MatPlotLib to plot a graph, where the plot changes over time. At every time step, an additional data point will be added to the plot. However, there should only be one graph displayed, whose appearance evolves over time.

在我的测试示例中,该图是一个简单的线性图(y = x).这是我尝试过的:

In my test example, the plot is a simple linear plot (y = x). Here is what I have tried:

for i in range(100):
    x = range(i)
    y = range(i)
    plt.plot(x, y)
    plt.ion()
    plt.show()
    time.sleep(1)

然而,这里发生的是创建了多个窗口,因此在循环结束时我有 100 个窗口.另外,我注意到对于最近的窗口,它只是一个白色窗口,并且该图仅出现在下一步中.

However, what happens here is that multiple windows are created, so that by the end of the loop I have 100 windows. Also, I have noticed that for the most recent window, it is just a white window, and the plot only appears on the next step.

所以,我的两个问题是:

So, my two questions are:

1)如何更改代码,以便仅显示一个窗口,其内容会随时间变化?

1) How can I change my code so that only a single window is displayed, whose contents changes over time?

2) 如何更改我的代码,以便对于最近的时间步长,绘图实际显示在窗口上,而不是只显示一个白色窗口?

2) How can I change my code so that for the most recent timestep, the plot is actually displayed on the window, rather than it only displaying a white window?

谢谢!

推荐答案

(1)

您可以在开头设置 plt.ion()并将所有图形绘制到同一窗口.在循环内使用 plt.draw() 显示图形,使用 plt.pause(t) 暂停.请注意, t 可能很小,但是该命令必须在此处才能使动画在大多数后端上正常工作.
您可能想在使用 plt.gca().cla()绘制新内容之前清除轴.

(1)

You can set plt.ion() at the beginning and plot all graphs to the same window. Within the loop use plt.draw() to show the graph and plt.pause(t) to make a pause. Note that t can be very small, but the command needs to be there for the animation to work on most backends.
You might want to clear the axes before plotting new content using plt.gca().cla().

import matplotlib.pyplot as plt

plt.ion()
for i in range(100):
    x = range(i)
    y = range(i)
    # plt.gca().cla() # optionally clear axes
    plt.plot(x, y)
    plt.title(str(i))
    plt.draw()
    plt.pause(0.1)

plt.show(block=True) # block=True lets the window stay open at the end of the animation.

对于这种非常简单的方法,也可以使用 http:/中提供的动画示例./matplotlib.org/examples/animation/index.html

Alternatively to this very simple approach, use any of the examples for animations provided in http://matplotlib.org/examples/animation/index.html

为了在新窗口中获取每个图,请使用 plt.figure() 并删除 plt.ion().也只显示最后的窗口:

In order to get each plot in a new window, use plt.figure() and remove plt.ion(). Also only show the windows at the end:

import matplotlib.pyplot as plt

for i in range(100):
    x = range(i)
    y = range(i)
    plt.figure()
    plt.plot(x, y)
    plt.title(str(i))
    
plt.show()

请注意,您可能会发现在这两种情况下,第一个图都是空的,因为对于 i=0range(i) == [] 是一个空列表没有任何意义.即使对于 i = 1 ,也只绘制了一个点,但是当然,没有线可以将单个点与自身连接.

Note that you might find that in both cases the first plot is empty simply because for i=0, range(i) == [] is an empty list without any points. Even for i=1 there is only one point being plotted, but of course no line can connect a single point with itself.

这篇关于使用 MatPlotLib 绘制连续的数据流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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