<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Using matplotlib in a python shell — Matplotlib 1.2.0 documentation</title> <link rel="stylesheet" href="../_static/mpl.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '1.2.0', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Matplotlib 1.2.0 documentation" href="../_static/opensearch.xml"/> <link rel="top" title="Matplotlib 1.2.0 documentation" href="../index.html" /> <link rel="up" title="User’s Guide" href="index.html" /> <link rel="next" title="Working with text" href="index_text.html" /> <link rel="prev" title="Customizing matplotlib" href="customizing.html" /> </head> <body> <!-- Piwik --> <script type="text/javascript"> if ("matplotlib.sourceforge.net" == document.location.hostname || "matplotlib.sf.net" == document.location.hostname) { var pkBaseURL = (("https:" == document.location.protocol) ? "https://apps.sourceforge.net/piwik/matplotlib/" : "http://apps.sourceforge.net/piwik/matplotlib/"); document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E")); } </script> <script type="text/javascript"> if ("matplotlib.sourceforge.net" == document.location.hostname || "matplotlib.sf.net" == document.location.hostname) { piwik_action_name = ''; piwik_idsite = 1; piwik_url = pkBaseURL + "piwik.php"; piwik_log(piwik_action_name, piwik_idsite, piwik_url); document.write(unescape('%3Cobject%3E%3Cnoscript%3E%3Cp%3E%3Cimg src="http://apps.sourceforge.net/piwik/matplotlib/piwik.php?idsite=1" alt="piwik"/%3E%3C/p%3E%3C/noscript%3E%3C/object%3E')); } </script> <!-- End Piwik Tag --> <link rel="shortcut icon" href="_static/favicon.ico"> <div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px"> <a href="../index.html"><img src="../_static/logo2.png" border="0" alt="matplotlib"/></a> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="index_text.html" title="Working with text" accesskey="N">next</a> |</li> <li class="right" > <a href="customizing.html" title="Customizing matplotlib" accesskey="P">previous</a> |</li> <li><a href="../index.html">home</a>| </li> <li><a href="../search.html">search</a>| </li> <li><a href="../examples/index.html">examples</a>| </li> <li><a href="../gallery.html">gallery</a>| </li> <li><a href="../contents.html">docs</a> »</li> <li><a href="index.html" accesskey="U">User’s Guide</a> »</li> </ul> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Using matplotlib in a python shell</a><ul> <li><a class="reference internal" href="#ipython-to-the-rescue">Ipython to the rescue</a></li> <li><a class="reference internal" href="#other-python-interpreters">Other python interpreters</a></li> <li><a class="reference internal" href="#controlling-interactive-updating">Controlling interactive updating</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="customizing.html" title="previous chapter">Customizing matplotlib</a></p> <h4>Next topic</h4> <p class="topless"><a href="index_text.html" title="next chapter">Working with text</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/users/shell.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="using-matplotlib-in-a-python-shell"> <span id="mpl-shell"></span><h1>Using matplotlib in a python shell<a class="headerlink" href="#using-matplotlib-in-a-python-shell" title="Permalink to this headline">¶</a></h1> <p>By default, matplotlib defers drawing until the end of the script because drawing can be an expensive operation, and you may not want to update the plot every time a single property is changed, only once after all the properties have changed.</p> <p>But when working from the python shell, you usually do want to update the plot with every command, eg, after changing the <a class="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.xlabel" title="matplotlib.pyplot.xlabel"><tt class="xref py py-func docutils literal"><span class="pre">xlabel()</span></tt></a>, or the marker style of a line. While this is simple in concept, in practice it can be tricky, because matplotlib is a graphical user interface application under the hood, and there are some tricks to make the applications work right in a python shell.</p> <div class="section" id="ipython-to-the-rescue"> <span id="ipython-pylab"></span><h2>Ipython to the rescue<a class="headerlink" href="#ipython-to-the-rescue" title="Permalink to this headline">¶</a></h2> <p>Fortunately, <a class="reference external" href="http://ipython.org/">ipython</a>, an enhanced interactive python shell, has figured out all of these tricks, and is matplotlib aware, so when you start ipython in the <em>pylab</em> mode.</p> <div class="highlight-ipython"><div class="highlight"><pre><span class="go">johnh@flag:~> ipython -pylab</span> <span class="go">Python 2.4.5 (#4, Apr 12 2008, 09:09:16)</span> <span class="go">IPython 0.9.0 -- An enhanced Interactive Python.</span> <span class="go"> Welcome to pylab, a matplotlib-based Python environment.</span> <span class="go"> For more information, type 'help(pylab)'.</span> <span class="gp">In [1]: </span><span class="n">x</span> <span class="o">=</span> <span class="n">randn</span><span class="p">(</span><span class="mi">10000</span><span class="p">)</span> <span class="gp">In [2]: </span><span class="n">hist</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span> </pre></div> </div> <p>it sets everything up for you so interactive plotting works as you would expect it to. Call <a class="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.figure" title="matplotlib.pyplot.figure"><tt class="xref py py-func docutils literal"><span class="pre">figure()</span></tt></a> and a figure window pops up, call <a class="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.plot" title="matplotlib.pyplot.plot"><tt class="xref py py-func docutils literal"><span class="pre">plot()</span></tt></a> and your data appears in the figure window.</p> <p>Note in the example above that we did not import any matplotlib names because in pylab mode, ipython will import them automatically. ipython also turns on <em>interactive</em> mode for you, which causes every pyplot command to trigger a figure update, and also provides a matplotlib aware <tt class="docutils literal"><span class="pre">run</span></tt> command to run matplotlib scripts efficiently. ipython will turn off interactive mode during a <tt class="docutils literal"><span class="pre">run</span></tt> command, and then restore the interactive state at the end of the run so you can continue tweaking the figure manually.</p> <p>There has been a lot of recent work to embed ipython, with pylab support, into various GUI applications, so check on the ipython mailing <a class="reference external" href="http://projects.scipy.org/mailman/listinfo/ipython-user">list</a> for the latest status.</p> </div> <div class="section" id="other-python-interpreters"> <span id="other-shells"></span><h2>Other python interpreters<a class="headerlink" href="#other-python-interpreters" title="Permalink to this headline">¶</a></h2> <p>If you can’t use ipython, and still want to use matplotlib/pylab from an interactive python shell, e.g. the plain-ole standard python interactive interpreter, you are going to need to understand what a matplotlib backend is <a class="reference internal" href="../faq/usage_faq.html#what-is-a-backend"><em>What is a backend?</em></a>.</p> <p>With the TkAgg backend, which uses the Tkinter user interface toolkit, you can use matplotlib from an arbitrary non-gui python shell. Just set your <tt class="docutils literal"><span class="pre">backend</span> <span class="pre">:</span> <span class="pre">TkAgg</span></tt> and <tt class="docutils literal"><span class="pre">interactive</span> <span class="pre">:</span> <span class="pre">True</span></tt> in your <tt class="file docutils literal"><span class="pre">matplotlibrc</span></tt> file (see <a class="reference internal" href="customizing.html#customizing-matplotlib"><em>Customizing matplotlib</em></a>) and fire up python. Then:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pylab</span> <span class="kn">import</span> <span class="o">*</span> <span class="gp">>>> </span><span class="n">plot</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span> <span class="gp">>>> </span><span class="n">xlabel</span><span class="p">(</span><span class="s">'hi mom'</span><span class="p">)</span> </pre></div> </div> <p>should work out of the box. This is also likely to work with recent versions of the qt4agg and gtkagg backends, and with the macosx backend on the Macintosh. Note, in batch mode, i.e. 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 via the <tt class="file docutils literal"><span class="pre">matplotlibrc</span></tt> file instead of using the functions listed in the next section.</p> <p>Gui shells are at best problematic, because they have to run a mainloop, but interactive plotting also involves a mainloop. Ipython has sorted all this out for the primary matplotlib backends. There may be other shells and IDEs that also work with matplotlib in interactive mode, but one obvious candidate does not: the python IDLE IDE is a Tkinter gui app that does not support pylab interactive mode, regardless of backend.</p> </div> <div class="section" id="controlling-interactive-updating"> <span id="controlling-interactive"></span><h2>Controlling interactive updating<a class="headerlink" href="#controlling-interactive-updating" title="Permalink to this headline">¶</a></h2> <p>The <em>interactive</em> property of the pyplot interface controls whether a figure canvas is drawn on every pyplot command. If <em>interactive</em> is <em>False</em>, then the figure state is updated on every plot command, but will only be drawn on explicit calls to <a class="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.draw" title="matplotlib.pyplot.draw"><tt class="xref py py-func docutils literal"><span class="pre">draw()</span></tt></a>. When <em>interactive</em> is <em>True</em>, then every pyplot command triggers a draw.</p> <p>The pyplot interface provides 4 commands that are useful for interactive control.</p> <dl class="docutils"> <dt><a class="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.isinteractive" title="matplotlib.pyplot.isinteractive"><tt class="xref py py-func docutils literal"><span class="pre">isinteractive()</span></tt></a></dt> <dd>returns the interactive setting <em>True|False</em></dd> <dt><a class="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.ion" title="matplotlib.pyplot.ion"><tt class="xref py py-func docutils literal"><span class="pre">ion()</span></tt></a></dt> <dd>turns interactive mode on</dd> <dt><a class="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.ioff" title="matplotlib.pyplot.ioff"><tt class="xref py py-func docutils literal"><span class="pre">ioff()</span></tt></a></dt> <dd>turns interactive mode off</dd> <dt><a class="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.draw" title="matplotlib.pyplot.draw"><tt class="xref py py-func docutils literal"><span class="pre">draw()</span></tt></a></dt> <dd>forces a figure redraw</dd> </dl> <p>When working with a big figure in which drawing is expensive, you may want to turn matplotlib’s interactive setting off temporarily to avoid the performance hit:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="c">#create big-expensive-figure</span> <span class="gp">>>> </span><span class="n">ioff</span><span class="p">()</span> <span class="c"># turn updates off</span> <span class="gp">>>> </span><span class="n">title</span><span class="p">(</span><span class="s">'now how much would you pay?'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">xticklabels</span><span class="p">(</span><span class="n">fontsize</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s">'green'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">draw</span><span class="p">()</span> <span class="c"># force a draw</span> <span class="gp">>>> </span><span class="n">savefig</span><span class="p">(</span><span class="s">'alldone'</span><span class="p">,</span> <span class="n">dpi</span><span class="o">=</span><span class="mi">300</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">close</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">ion</span><span class="p">()</span> <span class="c"># turn updating back on</span> <span class="gp">>>> </span><span class="n">plot</span><span class="p">(</span><span class="n">rand</span><span class="p">(</span><span class="mi">20</span><span class="p">),</span> <span class="n">mfc</span><span class="o">=</span><span class="s">'g'</span><span class="p">,</span> <span class="n">mec</span><span class="o">=</span><span class="s">'r'</span><span class="p">,</span> <span class="n">ms</span><span class="o">=</span><span class="mi">40</span><span class="p">,</span> <span class="n">mew</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">ls</span><span class="o">=</span><span class="s">'--'</span><span class="p">,</span> <span class="n">lw</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span> </pre></div> </div> </div> </div> </div> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="index_text.html" title="Working with text" >next</a> |</li> <li class="right" > <a href="customizing.html" title="Customizing matplotlib" >previous</a> |</li> <li><a href="../index.html">home</a>| </li> <li><a href="../search.html">search</a>| </li> <li><a href="../examples/index.html">examples</a>| </li> <li><a href="../gallery.html">gallery</a>| </li> <li><a href="../contents.html">docs</a> »</li> <li><a href="index.html" >User’s Guide</a> »</li> </ul> </div> <div class="footer"> © Copyright 2012 John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the matplotlib development team. Last updated on Jul 23, 2013. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>