Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pylab.ion() in python 2, matplotlib 1.1.1 and updating of the plot while the program runs

Tags:

what I am trying to do is having a script compute something, prepare a plot and show the already obtained results as a pylab.figure - in python 2 (specifically python 2.7) with a stable matplotlib (which is 1.1.1).

In python 3 (python 3.2.3 with a matplotlib git build ... version 1.2.x), this works fine. As a simple example (simulating a lengthy computation by time.sleep()) consider

import pylab import time import random  dat=[0,1] pylab.plot(dat) pylab.ion() pylab.draw()     for i in range (18):     dat.append(random.uniform(0,1))     pylab.plot(dat)     pylab.draw()     time.sleep(1) 

In python 2 (version 2.7.3 vith matplotlib 1.1.1), the code runs cleanly without errors but does not show the figure. A little trial and error with the python2 interpreter seemed to suggest to replace pylab.draw() with pylab.show(); doing this once is apparently sufficient (not, as with draw calling it after every change/addition to the plot). Hence:

import pylab import time import random  dat=[0,1] pylab.plot(dat) pylab.ion() pylab.show()     for i in range (18):     dat.append(random.uniform(0,1))     pylab.plot(dat)     #pylab.draw()     time.sleep(1) 

However, this doesn't work either. Again, it runs cleanly but does not show the figure. It seems to do so only when waiting for user input. It is not clear to me why this is, but the plot is finally shown when a raw_input() is added to the loop

import pylab import time import random  dat=[0,1] pylab.plot(dat) pylab.ion() pylab.show()     for i in range (18):     dat.append(random.uniform(0,1))     pylab.plot(dat)     #pylab.draw()     time.sleep(1)     raw_input() 

With this, the script will of course wait for user input while showing the plot and will not continue computing the data before the user hits enter. This was, of course, not the intention.

This may be caused by different versions of matplotlib (1.1.1 and 1.2.x) or by different python versions (2.7.3 and 3.2.3).

Is there any way to accomplish with python 2 with a stable (1.1.1) matplotlib what the above script (the first one) does in python 3, matplotlib 1.2.x: - computing data (which takes a while, in the above example simulated by time.sleep()) in a loop or iterated function and - (while still computing) showing what has already been computed in previous iterations - and not bothering the user to continually hit enter for the computation to continue

Thanks; I'd appreciate any help...

like image 425
0range Avatar asked Oct 10 '12 15:10

0range


People also ask

What is matplotlib PyLab Python?

Pylab is a module that provides a Matlab like namespace by importing functions from the modules Numpy and Matplotlib. Numpy provides efficient numerical vector calculations based on underlying Fortran and C binary libraries. Matplotlib contains functions to create visualizations of data.

What is the difference between PyLab and matplotlib?

PyLab is a procedural interface to the Matplotlib object-oriented plotting library. Matplotlib is the whole package; matplotlib. pyplot is a module in Matplotlib; and PyLab is a module that gets installed alongside Matplotlib. PyLab is a convenience module that bulk imports matplotlib.

What does PLT ion () do?

ion() in Python. Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack.

Is PLT show () necessary?

Plotting from an IPython shell Using plt. show() in Matplotlib mode is not required.

Where can Matplotlib be used?

Matplotlib can be used in Python scripts, the Python and IPython shells, the Jupyter notebook, web application servers, and four graphical user interface toolkits. Matplotlib tries to make easy things easy and hard things possible.

Is it possible to execute a python script using matplotlib?

I coded up a quick Python script using matplotlib, executed the script, only to not have the figure displayed to my screen. My script executed just fine. No error messages. No warnings. But there was still no plot to be found!

Should I use apt-get to install Matplotlib?

If you use apt-get to install matplotlib you lose control over what version of matplotlib you want to install — you simply have to use with whatever version is in the apt-get repository. This also muddles your system install of Python which I try to keep as clean as possible.

Who is the creator of Matplotlib?

Citing Matplotlib¶. Matplotlib is the brainchild of John Hunter (1968-2012), who, along with its many contributors, have put an immeasurable amount of time and effort into producing a piece of software utilized by thousands of scientists worldwide.


2 Answers

You want the pause function to give the gui framework a chance to re-draw the screen:

import pylab import time import random import matplotlib.pyplot as plt  dat=[0,1] fig = plt.figure() ax = fig.add_subplot(111) Ln, = ax.plot(dat) ax.set_xlim([0,20]) plt.ion() plt.show()     for i in range (18):     dat.append(random.uniform(0,1))     Ln.set_ydata(dat)     Ln.set_xdata(range(len(dat)))     plt.pause(1)      print 'done with loop' 

You don't need to create a new Line2D object every pass through, you can just update the data in the existing one.

Documentation:

pause(interval)     Pause for *interval* seconds.      If there is an active figure it will be updated and displayed,     and the gui event loop will run during the pause.      If there is no active figure, or if a non-interactive backend     is in use, this executes time.sleep(interval).      This can be used for crude animation. For more complex     animation, see :mod:`matplotlib.animation`.      This function is experimental; its behavior may be changed     or extended in a future release. 

A really over-kill method to is to use the matplotlib.animate module. On the flip side, it gives you a nice way to save the data if you want (ripped from my answer to Python- 1 second plots continous presentation).

example, api, tutorial

like image 50
tacaswell Avatar answered Sep 17 '22 00:09

tacaswell


Some backends (in my experience "Qt4Agg") require the pause function, as @tcaswell suggested.

Other backends (in my experience "TkAgg") seem to just update on draw() without requiring a pause. So another solution is to switch your backend, for example with matplotlib.use('TkAgg').

like image 37
hunse Avatar answered Sep 18 '22 00:09

hunse