python脚本ui,如何在Python程序的简单UI中显示实时图形?

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

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值