如何在Python程序的简单UI中显示实时图表?
-
29-09-2019 - |
题
我有一种复杂的算法,该算法更新了3个存储在数组中的直方图。我想调试我的算法,因此我想在用户界面中将数组显示为直方图。什么是最简单的方法。 (快速应用程序开发比优化的代码更重要。)
我在QT(C ++)和Matplotlib方面有一些经验。
(我将把这个问题开放一两天,因为我很难评估解决方案而没有更多我没有的经验。希望社区的票数将有助于选择最佳答案。)
解决方案
编辑:如今,使用更容易,更好地使用 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帧,而使用代码i每秒4帧 首先发布. 。诀窍是避免要求matplotlib绘制完整的数字。而是致电 plt.hist
一次,然后操纵现有 matplotlib.patches.Rectangle
s in patches
更新直方图并致电fig.canvas.draw()
使更新可见。
其他提示
对于实时绘图,我建议您尝试Chaco,Pyqtgraph或任何基于OpenGL的库,例如笨拙或Visvis。 Matplotlib,尽管如此,Matplotlib通常不适合这种应用。
编辑: 笨拙,Visvis,Galry和Pyqtgraph的开发人员都在可视化库上合作 Vispy. 。它仍在开发的早期,但很有希望,而且已经很强大。
我建议在交互式模式下使用matplotlib,如果您致电 .show
一旦它将在自己的窗口中弹出,如果您不存在,则仅存在于内存中,并且完成后可以写入文件。
哦,现在看,当您说实时时,您意味着要高于5 Hz Matplotlib的刷新率不会完成这项工作。我以前有这个问题,我去了 pyqwt 与Pyqt一起使用。
不隶属于 StackOverflow