20

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

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

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

4

4 回答 4

23

编辑:如今,使用起来更容易更好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一次,然后操作现有matplotlib.patches.Rectangle的 spatches以更新直方图,并调用 fig.canvas.draw()以使更新可见。

于 2010-11-09T02:37:15.567 回答
13

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

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

于 2013-02-09T21:21:18.283 回答
1

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

于 2010-11-09T01:48:19.753 回答
0

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

于 2010-11-09T01:49:03.650 回答