使用 Python 创建动态更新图
我需要用Python编写一个脚本,该脚本将获取动态更改的数据,数据源在这里并不重要,并在屏幕上显示图形。
我知道如何使用 matplotlib,但 matplotlib 的问题是我只能在脚本末尾显示一次图形。我不仅需要能够一次显示图表,而且还需要在每次数据发生变化时即时更新它。
我发现可以使用 wxPython 和 matplotlib 来执行此操作,但对我来说执行此操作有点复杂,因为我根本不熟悉 wxPython。
因此,如果有人向我展示如何使用 wxPython 和 matplotlib 来显示和更新简单图形的简单示例,我将非常高兴。或者,如果有其他方法可以做到这一点,那对我也有好处。
更新
PS:因为没有人回答并查看 @janislaw 注意到的 matplotlib 帮助并编写了一些代码。这是一些虚拟示例:
import time
import matplotlib.pyplot as plt
def data_gen():
a=data_gen.a
if a>10:
data_gen.a=1
data_gen.a=data_gen.a+1
return range (a,a+10)
def run(*args):
background = fig.canvas.copy_from_bbox(ax.bbox)
while 1:
time.sleep(0.1)
# restore the clean slate background
fig.canvas.restore_region(background)
# update the data
ydata = data_gen()
xdata=range(len(ydata))
line.set_data(xdata, ydata)
# just draw the animated artist
ax.draw_artist(line)
# just redraw the axes rectangle
fig.canvas.blit(ax.bbox)
data_gen.a=1
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([], [], animated=True)
ax.set_ylim(0, 20)
ax.set_xlim(0, 10)
ax.grid()
manager = plt.get_current_fig_manager()
manager.window.after(100, run)
plt.show()
此实现存在问题,例如如果您尝试移动窗口,脚本就会停止。不过基本上是可以用的。
I need to write a script in Python that will take dynamically changed data, the source of data is not matter here, and display graph on the screen.
I know how to use matplotlib, but the problem with matplotlib is that I can display graph only once, at the end of the script. I need to be able not only to display graph one time, but also update it on the fly, each time when data changes.
I found that it is possible to use wxPython with matplotlib to do this, but it is little bit complicate to do this for me, because I am not familiar with wxPython at all.
So I will be very happy if someone will show me simple example how to use wxPython with matplotlib to show and update simple graph. Or, if it is some other way to do this, it will be good to me too.
Update
PS: since no one answered and looked at matplotlib help noticed by @janislaw and wrote some code. This is some dummy example:
import time
import matplotlib.pyplot as plt
def data_gen():
a=data_gen.a
if a>10:
data_gen.a=1
data_gen.a=data_gen.a+1
return range (a,a+10)
def run(*args):
background = fig.canvas.copy_from_bbox(ax.bbox)
while 1:
time.sleep(0.1)
# restore the clean slate background
fig.canvas.restore_region(background)
# update the data
ydata = data_gen()
xdata=range(len(ydata))
line.set_data(xdata, ydata)
# just draw the animated artist
ax.draw_artist(line)
# just redraw the axes rectangle
fig.canvas.blit(ax.bbox)
data_gen.a=1
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([], [], animated=True)
ax.set_ylim(0, 20)
ax.set_xlim(0, 10)
ax.grid()
manager = plt.get_current_fig_manager()
manager.window.after(100, run)
plt.show()
This implementation have problems, like script stops if you trying to move the window. But basically it can be used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是我写的一个处理这个问题的类。它需要您传递给它的 matplotlib 图形并将其放置在 GUI 窗口中。它位于自己的线程中,因此即使您的程序很忙,它也能保持响应。
在您的代码中,您需要像这样初始化绘图仪:
然后您可以像这样绘制它:
Here is a class I wrote that handles this issue. It takes a matplotlib figure that you pass to it and places it in a GUI window. Its in its own thread so that it stays responsive even when your program is busy.
In your code, you need to initialize the plotter like this:
Then you can plot to it like this:
作为 matplotlib 的替代品,Chaco 库提供了很好的绘图功能,并且在某些方面更适合实时绘图。
请参阅此处的一些屏幕截图,特别是请参阅以下示例:
data_stream.py
spectrum.py
Chaco 有 qt 和 wx 的后端,所以大多数时候它都能很好地为您处理底层细节。
As an alternative to matplotlib, the Chaco library provides nice graphing capabilities and is in some ways better-suited for live plotting.
See some screenshots here, and in particular, see these examples:
data_stream.py
spectrum.py
Chaco has backends for qt and wx, so it handles the underlying details for you rather nicely most of the time.
您可以使用
matplotlib.pyplot.show(block=False) 来代替
matplotlib.pyplot.show()
。该调用不会阻止程序进一步执行。Instead of
matplotlib.pyplot.show()
you can just usematplotlib.pyplot.show(block=False)
. This call will not block the program to execute further.动态绘图的示例,秘密是在绘图时暂停,这里我使用networkx:
example of dynamic plot , the secret is to do a pause while plotting , here i use networkx:
我需要创建一个随时间更新的图表。我想出的最方便的解决方案是每次创建一个新图表。问题是创建第一个图表后脚本将不会执行,除非手动关闭窗口。
通过打开交互模式避免了这个问题,如下所示。
这里有两点需要注意
I had the need to create a graph that updates with time. The most convenient solution I came up was to create a new graph each time. The issue was that the script won't be executed after the first graph is created, unless the window is closed manually.
That issue was avoided by turning the interactive mode on as shown below
There are two important points to note here
我创建了一个类,它使用 matplotlib 图绘制 tkinter 小部件。绘图是动态更新的(或多或少是实时的)。
下面是一个自定义 tkinter 比例的示例,用于更新 tkinter 图中绘制的数据。
上面的示例生成以下窗口。
I have created a class that draws a tkinter widget with a matplotlib plot. The plot is updated dynamically (more or less in realtime).
Below is an example of a custom tkinter scale used to update data which is drawn in the tkinter plot.
The example above produces the following window.