|
From: David H. <dh...@gm...> - 2013-03-11 17:55:22
|
Someone may have to correct me, but I think this has to do with the Qt4
event loop and it not being run properly. When you get into real time
plotting it can get kind of tricky. In your case (I got the same
results). I have made real-time PyQt4 GUIs before and have always used
separate QThreads and Qt signals/slots to update the plot. I've never
used GTK so I'm not sure why that worked vs Qt, I would think they would
use similar principles but matplotlib does some magic behind the scenes
sometimes. You can see different results if you comment out the while
loop and import the module into your python/ipython interpreter. After
doing this you'll see the figure pop up (you don't even need the
fig.canvas.show() for this part if interactive mode is on. I went one
step further and turned the while loop into a function:
def one_iter(i):
# Contents of while loop
Calling this in the interpreter shows the figure updating after each
call, but running in a loop (even with sleep) won't show any updates
until the loop is done. In my opinion you have a few choices that really
depend on your programming comfort level:
1. Don't make a real-time plot.
Do you really need a real-time plot that updates from some
external source?
2. Maybe you should look at the matplotlib animation functionality
(http://matplotlib.org/api/animation_api.html). I like this tutorial:
http://jakevdp.github.com/blog/2012/08/18/matplotlib-animation-tutorial/. This
won't get you a real-time GUI exactly, but it can help if what you're
doing isn't too complicated. It can also be nice for making videos of
plot animations.
3. If you need a GUI with multiple plots and you need for future feature
creep, I would research making PyQt4 GUIs, QThreads, Qt signals and
slots, and putting matplotlib figures into a PyQt4 GUI. This is complex
if you are not familiar with GUI programming and will take a while.
Sorry I couldn't be of more help, but it really depends on what exactly
you are doing. Mainly, what do you mean by real-time? Do you really
mean animation? Let me know what you come up with, I'm interested.
-Dave
P.S. Why use a while loop? You can do the same thing with:
for i in range(1000):
# Do stuff
On 3/11/13 10:34 AM, Neal Becker wrote:
> I added fig.canvas.show(). It still does nothing.
>
> If I add
> mpl.use ('GTK'), now it seems to be doing realtime plotting.
>
> import matplotlib as mpl
>
> import matplotlib.pyplot as plt
> plt.ion()
> import numpy as np
> fig=plt.figure()
> plt.axis([0,1000,0,1])
>
> i=0
> x=list()
> y=list()
>
> fig.canvas.show()
> while i <1000:
> temp_y=np.random.random()
> x.append(i)
> y.append(temp_y)
> plt.scatter(i,temp_y)
> i+=1
> plt.draw()
>
>
>
> On Mon, Mar 11, 2013 at 10:35 AM, David Hoese <dh...@gm...
> <mailto:dh...@gm...>> wrote:
>
> Oops forgot to change the subject line.
>
> On 3/11/13 9:34 AM, David Hoese wrote:
>
> You likely need to "show()" the canvas. I usually do this by
> calling "fig.canvas.show()" before the for loop.
> Since you are using a Qt4 backend the canvas used by the
> figure is a QWidget, the basic component of a Qt4 GUI. I don't
> know if there is a more matplotlib specific way of doing this,
> but when dealing with a larger system this is how I do it.
>
> I would also add a sleep ("from time import sleep") of a
> couple seconds for testing to make sure you are getting
> through the entire for loop before you can see it.
>
> Please CC in any replies, thanks.
>
> -Dave
>
>
> On 3/11/13 8:58 AM, ndb...@gm...
> <mailto:ndb...@gm...> wrote:
>
> I want to update a plot in real time. I did some goog
> search, and saw various
> answers. Trouble is, they aren't working.
>
> Here's a typical example:
>
> import matplotlib.pyplot as plt
> import numpy as np
> fig=plt.figure()
> plt.axis([0,1000,0,1])
>
> i=0
> x=list()
> y=list()
>
> while i <1000:
> temp_y=np.random.random()
> x.append(i)
> y.append(temp_y)
> plt.scatter(i,temp_y)
> i+=1
> plt.draw()
>
> If I run this, it draws nothing.
>
> This is my matplotlibrc:
> backend : Qt4Agg
> mathtext.fontset: stix
>
>
>
>
|