71 lines (54 with data), 2.5 kB
@header@
<h2>Using matplotlib interactively</h2>
By default, matplotlib defers drawing until the end of the script
because drawing can be an expensive opertation, and in often you don't
want to update the plot every time a single property is changed, only
once after all the properties have changed.
But in interactive mode, eg from the python shell, you usually do want
to update the plot with every command, eg, after changing the xlabel
or the marker style of a line. With the <a
href=backends.html#TkAgg>TkAgg</a> backend, you can use matplotlib
from an arbitrary python shell. Just set TkAgg to be your default
backend and interactive to be True in your <a
href=.matplotlibrc>matplotlibrc</a> file and fire up python. Then
<pre>
>>> from matplotlib.matlab import *
>>> plot([1,2,3])
>>> xlabel('hi mom')
</pre>
should work out of the box. Note, in batch mode, ie when making
figures from scripts, interactive mode can be slow since it redraws
the figure with each command. So you may want to think carefully
before making this the default behavior. TkAgg sets interactive mode
to True when you issue the <a
href=matplotlib.matlab.html#-show>show</a> command.<p>
Unfortunately, due to the 'mainloop' cycle of GUI toolkits, it is not
yet possible to use matplotlib from an arbitrary python shell with the
other GUI backends. Instead, there are custom solutions depending on
the GUI environment in which you use matplotlib.<p>
The recommended way to use matplotlib interactively from a shell is
with <a href=http://ipython.scipy.org>ipython</a>, which has an pylab
mode that detects your matplotlib <a
href=.matplotlibrc>matplotlibrc</a> file and makes the right settings
to run matplotlib with your GUI of choice in interactive mode using
threading. gtk users will need to make sure that they have compiled
gtk with threading for this to work.<p>
<h3>WX and pycrust</h3>
It is possible to use matplotlib with custom GUI shells, eg pycrust
for wxpython. With a WX shell, such as pycrust, you need to put
matplotlib in interactive mode.
<pre>
>>> import matplotlib
>>> matplotlib.interactive(True)
>>> matplotlib.use('WX')
>>> from matplotlib.matlab import *
>>> plot([1,2,3])
>>> xlabel('time (s)')
</pre>
If you primarily want to use matplotlib interactively in a wx shell,
set the following in your <a href=.matplotlibrc>.matplotlibrc</a> file
<pre>
backend : Wx
interactive : True
</pre>
@footer@