如何在简单的UI中为python程序显示实时图形? [英] How do I display real-time graphs in a simple UI for a python program?
问题描述
我有一个复杂的算法,可以更新存储在数组中的3个直方图.我想调试算法,因此我想在用户界面中将数组显示为直方图.什么是最简单的方法. (快速的应用程序开发比优化的代码更重要.)
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.)
我对Qt(在C ++中)有一些经验,对matplotlib也有一些经验.
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.)
推荐答案
如今,使用matplotlib.animation
更容易,更好:
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.Rectangle
s in patches
to update the histogram, and call
fig.canvas.draw()
to make the updates visible.
这篇关于如何在简单的UI中为python程序显示实时图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!