20

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

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
Neil G
  • 32,138
  • 39
  • 156
  • 257
  • If rapid development is what you're after, I'd recommend Tkinter. It's far more intuitive IMO than PyQt. – Rafe Kettler Nov 09 '10 at 01:31
  • I don't see the difficulty. Just use matplotlib to plot the histograms. Any particular issue? – Bernardo Kyotoku Nov 09 '10 at 01:38
  • @Bernardo, I want them to update as the underlying data structures are updated. If I plot using matplotlib, I'll get a graph for every time step? That is going to be hard to keep track of when I have a window for every time step. – Neil G Nov 09 '10 at 01:46
  • 2
    @Neil: There is an example of making an animated graph here: http://stackoverflow.com/questions/4098131/matplotlib-update-a-plot/4098938#4098938 . Perhaps that will help you? – unutbu Nov 09 '10 at 01:54
  • 2
    @unutbu nice +1, why didn't you put as an answer? – Bernardo Kyotoku Nov 09 '10 at 02:05
  • 1
    @Bernardo: It's Joe Kington's answer, so he deserves the credit :) – unutbu Nov 09 '10 at 02:15
  • 1
    @unutbu Please add it as he's not here to add it. Anyway, you deserve the credit for reading the question and making the connection. – Neil G Nov 09 '10 at 02:24

4 Answers4

23

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.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • This works well but the plotting window turns not responding or closes on its own.. any suggestion? – DevC Feb 12 '14 at 12:45
  • @DevC: Nowadays, it is better to use `matplotlib.animation`. I've added an example, above. – unutbu Feb 12 '14 at 13:05
  • Here you can find example animation videos: http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ – kakyo Sep 10 '14 at 20:29
  • how is this done with real time emotion detection using webcam with opencv python? – Mr. Jibz Oct 02 '18 at 15:39
13

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.

Neil G
  • 32,138
  • 39
  • 156
  • 257
Luke
  • 11,374
  • 2
  • 48
  • 61
  • The [vispy website](http://vispy.org) includes a gallery of several applications and the code corresponding to these applications. – Mark Nov 27 '14 at 18:43
1

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.

Gareth Davidson
  • 4,857
  • 2
  • 26
  • 45
0

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.

Bernardo Kyotoku
  • 459
  • 4
  • 14
  • 1
    PyQwt is currently considered unmaintained. Hopefully that will change in the future, but beware for now.. – Luke Feb 09 '13 at 21:19