如何在 Python 程序的简单 UI 中显示实时图形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4129697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:22:32  来源:igfitidea点击:

How do I display real-time graphs in a simple UI for a python program?

pythonmatplotlibpyqtvispy

提问by Neil G

I have a complicated algorithm that updates 3 histograms that are stored in arrays. I want to debug my algorithm, so I was thinking of showing the arrays as histograms in a user interface. What is the easiest way to do this. (Rapid application development is more important than optimized code.)

我有一个复杂的算法,可以更新存储在数组中的 3 个直方图。我想调试我的算法,所以我想在用户界面中将数组显示为直方图。什么是最简单的方法来做到这一点。(快速应用程序开发比优化代码更重要。)

I have some experience with Qt (in C++) and some experience with matplotlib.

我有一些 Qt 的经验(在 C++ 中)和一些 matplotlib 的经验。

(I'm going to leave this question open for a day or two because it's hard for me to evaluate the solutions without a lot more experience that I don't have. Hopefully, the community's votes will help choose the best answer.)

(我将把这个问题留待一两天,因为如果没有更多我没有的经验,我很难评估解决方案。希望社区的投票将有助于选择最佳答案。)

采纳答案by unutbu

Edit: Nowadays, it is easier and better to use matplotlib.animation:

编辑:如今,使用起来更容易、更好matplotlib.animation

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation


def animate(frameno):
    x = mu + sigma * np.random.randn(10000)
    n, _ = np.histogram(x, bins, normed=True)
    for rect, h in zip(patches, n):
        rect.set_height(h)
    return patches    

mu, sigma = 100, 15
fig, ax = plt.subplots()
x = mu + sigma * np.random.randn(10000)
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='green', alpha=0.75)

ani = animation.FuncAnimation(fig, animate, blit=True, interval=10,
                              repeat=True)
plt.show()


There is an example of making an animated graph here. Building on this example, you might try something like:

有制作一个动画图形的例子在这里。在此示例的基础上,您可以尝试以下操作:

import numpy as np
import matplotlib.pyplot as plt

plt.ion()
mu, sigma = 100, 15
fig = plt.figure()
x = mu + sigma*np.random.randn(10000)
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='green', alpha=0.75)
for i in range(50):
    x = mu + sigma*np.random.randn(10000)
    n, bins = np.histogram(x, bins, normed=True)
    for rect,h in zip(patches,n):
        rect.set_height(h)
    fig.canvas.draw()

I can get about 14 frames per second this way, compared to 4 frames per second using the code I first posted. The trick is to avoid asking matplotlib to draw complete figures. Instead call plt.histonce, then manipulate the existing matplotlib.patches.Rectangles in patchesto update the histogram, and call fig.canvas.draw()to make the updates visible.

我可以通过这种方式每秒获得大约 14 帧,而使用我第一次发布的代码每秒可以获得 4 帧。诀窍是避免要求 matplotlib 绘制完整的图形。而是调用plt.hist一次,然后操作现有的matplotlib.patches.Rectanglespatches以更新直方图,并调用 fig.canvas.draw()以使更新可见。

回答by Gareth Davidson

I recommend using matplotlib in interactive mode, if you call .showonce then it will pop up in its own window, if you don't then it exists only in memory and can be written to a file when you're done with it.

我建议在交互模式下使用 matplotlib,如果你调用.show一次,那么它会在它自己的窗口中弹出,如果你不这样做,那么它只存在于内存中,并且可以在你完成后写入文件。

回答by Bernardo Kyotoku

Ouh, now see, when you say real time you mean you want a refresh rate higher than 5 Hz matplotlib won't do the job. I had this problem before, I went for PyQwtthat works with PyQt.

哦,现在明白了,当你说实时时,你的意思是你想要高于 5 Hz 的刷新率 matplotlib 无法完成这项工作。我以前遇到过这个问题,我选择了与 PyQt一起使用的PyQwt

回答by Luke

For realtime plotting, I recommend trying Chaco, pyqtgraph, or any of the opengl-based libraries like glumpy or visvis. Matplotlib, wonderful as it is, is generally not suitable for this kind of application.

对于实时绘图,我建议尝试 Chaco、pyqtgraph 或任何基于 opengl 的库,如 glumpy 或 visvis。Matplotlib 虽然很棒,但通常不适合这种应用程序。

Edit:the developers of glumpy, visvis, galry, and pyqtgraph are all collaborating on a visualization library called vispy. It is still early in development, but promising and already quite powerful.

编辑:glumpy、visvis、galry 和 pyqtgraph 的开发人员都在合作开发一个名为vispy的可视化库。它仍处于开发初期,但很有前途并且已经非常强大。