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

发布于 2024-10-01 05:42:24 字数 215 浏览 4 评论 0原文

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

我有一些使用 Qt(C++)的经验和一些 matplotlib 的经验。

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

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

I have some experience with Qt (in C++) and some experience with 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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

毁梦 2024-10-08 05:42:25

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

I recommend using matplotlib in interactive mode, if you call .show once 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.

风月客 2024-10-08 05:42:25

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

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 PyQwt that works with PyQt.

甜柠檬 2024-10-08 05:42:24

编辑:如今,使用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()

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

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

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

Edit: Nowadays, it is easier and better to use 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.hist once, then manipulate the existing matplotlib.patches.Rectangles in patches to update the histogram, and call
fig.canvas.draw() to make the updates visible.

爱冒险 2024-10-08 05:42:24

对于实时绘图,我建议尝试 Chaco、pyqtgraph 或任何基于 opengl 的库,如 glumpy 或 visvis。 Matplotlib 尽管很精彩,但通常不适合此类应用。

编辑: glumpy、visvis、galry 和 pyqtgraph 的开发人员都在合作开发一个名为 vispy 的可视化库。它仍处于开发早期,但前景光明并且已经相当强大。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文