<!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>Usage — Matplotlib v1.1.1 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.1.1', 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 v1.1.1 documentation" href="../_static/opensearch.xml"/> <link rel="top" title="Matplotlib v1.1.1 documentation" href="../index.html" /> <link rel="up" title="The Matplotlib FAQ" href="index.html" /> <link rel="next" title="How-To" href="howto_faq.html" /> <link rel="prev" title="Installation" href="installing_faq.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="howto_faq.html" title="How-To" accesskey="N">next</a> |</li> <li class="right" > <a href="installing_faq.html" title="Installation" 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">The Matplotlib FAQ</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="#">Usage</a><ul> <li><a class="reference internal" href="#general-concepts">General Concepts</a></li> <li><a class="reference internal" href="#matplotlib-pylab-and-pyplot-how-are-they-related">Matplotlib, pylab, and pyplot: how are they related?</a></li> <li><a class="reference internal" href="#coding-styles">Coding Styles</a></li> <li><a class="reference internal" href="#what-is-a-backend">What is a backend?</a></li> <li><a class="reference internal" href="#what-is-interactive-mode">What is interactive mode?</a><ul> <li><a class="reference internal" href="#interactive-example">Interactive example</a></li> <li><a class="reference internal" href="#non-interactive-example">Non-interactive example</a></li> <li><a class="reference internal" href="#summary">Summary</a></li> </ul> </li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="installing_faq.html" title="previous chapter">Installation</a></p> <h4>Next topic</h4> <p class="topless"><a href="howto_faq.html" title="next chapter">How-To</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/faq/usage_faq.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" size="18" /> <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="usage"> <span id="usage-faq"></span><h1>Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h1> <div class="contents topic" id="contents"> <p class="topic-title first">Contents</p> <ul class="simple"> <li><a class="reference internal" href="#usage" id="id4">Usage</a><ul> <li><a class="reference internal" href="#general-concepts" id="id5">General Concepts</a></li> <li><a class="reference internal" href="#matplotlib-pylab-and-pyplot-how-are-they-related" id="id6">Matplotlib, pylab, and pyplot: how are they related?</a></li> <li><a class="reference internal" href="#coding-styles" id="id7">Coding Styles</a></li> <li><a class="reference internal" href="#what-is-a-backend" id="id8">What is a backend?</a></li> <li><a class="reference internal" href="#what-is-interactive-mode" id="id9">What is interactive mode?</a><ul> <li><a class="reference internal" href="#interactive-example" id="id10">Interactive example</a></li> <li><a class="reference internal" href="#non-interactive-example" id="id11">Non-interactive example</a></li> <li><a class="reference internal" href="#summary" id="id12">Summary</a></li> </ul> </li> </ul> </li> </ul> </div> <div class="section" id="general-concepts"> <span id="id1"></span><h2>General Concepts<a class="headerlink" href="#general-concepts" title="Permalink to this headline">¶</a></h2> <p><a class="reference internal" href="../api/matplotlib_configuration_api.html#module-matplotlib" title="matplotlib"><tt class="xref py py-mod docutils literal"><span class="pre">matplotlib</span></tt></a> has an extensive codebase that can be daunting to many new users. However, most of matplotlib can be understood with a fairly simple conceptual framework and knowledge of a few important points.</p> <p>Plotting requires action on a range of levels, from the most general (e.g., ‘contour this 2-D array’) to the most specific (e.g., ‘color this screen pixel red’). The purpose of a plotting package is to assist you in visualizing your data as easily as possible, with all the necessary control – that is, by using relatively high-level commands most of the time, and still have the ability to use the low-level commands when needed.</p> <p>Therefore, everything in matplotlib is organized in a hierarchy. At the top of the hierarchy is the matplotlib “state-machine environment” which is provided by the <a class="reference internal" href="../api/pyplot_api.html#module-matplotlib.pyplot" title="matplotlib.pyplot"><tt class="xref py py-mod docutils literal"><span class="pre">matplotlib.pyplot</span></tt></a> module. At this level, simple functions are used to add plot elements (lines, images, text, etc.) to the current axes in the current figure.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Pyplot’s state-machine environment behaves similarly to MATLAB and should be most familiar to users with MATLAB experience.</p> </div> <p>The next level down in the hierarchy is the first level of the object-oriented interface, in which pyplot is used only for a few functions such as figure creation, and the user explicitly creates and keeps track of the figure and axes objects. At this level, the user uses pyplot to create figures, and through those figures, one or more axes objects can be created. These axes objects are then used for most plotting actions.</p> <p>For even more control – which is essential for things like embedding matplotlib plots in GUI applications – the pyplot level may be dropped completely, leaving a purely object-oriented approach.</p> </div> <div class="section" id="matplotlib-pylab-and-pyplot-how-are-they-related"> <span id="pylab"></span><h2>Matplotlib, pylab, and pyplot: how are they related?<a class="headerlink" href="#matplotlib-pylab-and-pyplot-how-are-they-related" title="Permalink to this headline">¶</a></h2> <p>Matplotlib is the whole package; <tt class="xref py py-mod docutils literal"><span class="pre">pylab</span></tt> is a module in matplotlib that gets installed alongside <a class="reference internal" href="../api/matplotlib_configuration_api.html#module-matplotlib" title="matplotlib"><tt class="xref py py-mod docutils literal"><span class="pre">matplotlib</span></tt></a>; and <a class="reference internal" href="../api/pyplot_api.html#module-matplotlib.pyplot" title="matplotlib.pyplot"><tt class="xref py py-mod docutils literal"><span class="pre">matplotlib.pyplot</span></tt></a> is a module in matplotlib.</p> <p>Pyplot provides the state-machine interface to the underlying plotting library in matplotlib. This means that figures and axes are implicitly and automatically created to achieve the desired plot. For example, calling <tt class="docutils literal"><span class="pre">plot</span></tt> from pyplot will automatically create the necessary figure and axes to achieve the desired plot. Setting a title will then automatically set that title to the current axes object:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span> <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span> <span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s">"Simple Plot"</span><span class="p">)</span> <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span> </pre></div> </div> <p>Pylab combines the pyplot functionality (for plotting) with the numpy functionality (for mathematics and for working with arrays) in a single namespace, making that namespace (or environment) even more MATLAB-like. For example, one can call the <cite>sin</cite> and <cite>cos</cite> functions just like you could in MATLAB, as well as having all the features of pyplot.</p> <p>The pyplot interface is generally preferred for non-interactive plotting (i.e., scripting). The pylab interface is convenient for interactive calculations and plotting, as it minimizes typing. Note that this is what you get if you use the <em>ipython</em> shell with the <em>-pylab</em> option, which imports everything from pylab and makes plotting fully interactive.</p> </div> <div class="section" id="coding-styles"> <span id="id2"></span><h2>Coding Styles<a class="headerlink" href="#coding-styles" title="Permalink to this headline">¶</a></h2> <p>When viewing this documentation and examples, you will find different coding styles and usage patterns. These styles are perfectly valid and have their pros and cons. Just about all of the examples can be converted into another style and achieve the same results. The only caveat is to avoid mixing the coding styles for your own code.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Developers for matplotlib have to follow a specific style and guidelines. See <a class="reference internal" href="../devel/index.html#developers-guide-index"><em>The Matplotlib Developers’ Guide</em></a>.</p> </div> <p>Of the different styles, there are two that are officially supported. Therefore, these are the preferred ways to use matplotlib.</p> <p>For the preferred pyplot style, the imports at the top of your scripts will typically be:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span> </pre></div> </div> <p>Then one calls, for example, np.arange, np.zeros, np.pi, plt.figure, plt.plot, plt.show, etc. So, a simple example in this style would be:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span> <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">)</span> <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span> </pre></div> </div> <p>Note that this example used pyplot’s state-machine to automatically and implicitly create a figure and an axes. For full control of your plots and more advanced usage, use the pyplot interface for creating figures, and then use the object methods for the rest:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span> <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">)</span> <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_subplot</span><span class="p">(</span><span class="mi">111</span><span class="p">)</span> <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span> </pre></div> </div> <p>Next, the same example using a pure MATLAB-style:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pylab</span> <span class="kn">import</span> <span class="o">*</span> <span class="n">x</span> <span class="o">=</span> <span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">)</span> <span class="n">y</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="n">show</span><span class="p">()</span> </pre></div> </div> <p>So, why all the extra typing as one moves away from the pure MATLAB-style? For very simple things like this example, the only advantage is academic: the wordier styles are more explicit, more clear as to where things come from and what is going on. For more complicated applications, this explicitness and clarity becomes increasingly valuable, and the richer and more complete object-oriented interface will likely make the program easier to write and maintain.</p> </div> <div class="section" id="what-is-a-backend"> <span id="id3"></span><h2>What is a backend?<a class="headerlink" href="#what-is-a-backend" title="Permalink to this headline">¶</a></h2> <p>A lot of documentation on the website and in the mailing lists refers to the “backend” and many new users are confused by this term. matplotlib targets many different use cases and output formats. Some people use matplotlib interactively from the python shell and have plotting windows pop up when they type commands. Some people embed matplotlib into graphical user interfaces like wxpython or pygtk to build rich applications. Others use matplotlib in batch scripts to generate postscript images from some numerical simulations, and still others in web application servers to dynamically serve up graphs.</p> <p>To support all of these use cases, matplotlib can target different outputs, and each of these capabilities is called a backend; the “frontend” is the user facing code, ie the plotting code, whereas the “backend” does all the hard work behind-the-scenes to make the figure. There are two types of backends: user interface backends (for use in pygtk, wxpython, tkinter, qt, macosx, or fltk; also referred to as “interactive backends”) and hardcopy backends to make image files (PNG, SVG, PDF, PS; also referred to as “non-interactive backends”).</p> <p>There are a two primary ways to configure your backend. One is to set the <tt class="docutils literal"><span class="pre">backend</span></tt> parameter in your <tt class="docutils literal"><span class="pre">matplotlibrc</span></tt> file (see <a class="reference internal" href="../users/customizing.html#customizing-matplotlib"><em>Customizing matplotlib</em></a>):</p> <div class="highlight-python"><pre>backend : WXAgg # use wxpython with antigrain (agg) rendering</pre> </div> <p>The other is to use the matplotlib <a class="reference internal" href="../api/matplotlib_configuration_api.html#matplotlib.use" title="matplotlib.use"><tt class="xref py py-func docutils literal"><span class="pre">use()</span></tt></a> directive:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">matplotlib</span> <span class="n">matplotlib</span><span class="o">.</span><span class="n">use</span><span class="p">(</span><span class="s">'PS'</span><span class="p">)</span> <span class="c"># generate postscript output by default</span> </pre></div> </div> <p>If you use the <tt class="docutils literal"><span class="pre">use</span></tt> directive, this must be done before importing <a class="reference internal" href="../api/pyplot_api.html#module-matplotlib.pyplot" title="matplotlib.pyplot"><tt class="xref py py-mod docutils literal"><span class="pre">matplotlib.pyplot</span></tt></a> or <tt class="xref py py-mod docutils literal"><span class="pre">matplotlib.pylab</span></tt>.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Backend name specifications are not case-sensitive; e.g., ‘GTKAgg’ and ‘gtkagg’ are equivalent.</p> </div> <p>With a typical installation of matplotlib, such as from a binary installer or a linux distribution package, a good default backend will already be set, allowing both interactive work and plotting from scripts, with output to the screen and/or to a file, so at least initially you will not need to use either of the two methods given above.</p> <p>If, however, you want to write graphical user interfaces, or a web application server (<a class="reference internal" href="howto_faq.html#howto-webapp"><em>Matplotlib in a web application server</em></a>), or need a better understanding of what is going on, read on. To make things a little more customizable for graphical user interfaces, matplotlib separates the concept of the renderer (the thing that actually does the drawing) from the canvas (the place where the drawing goes). The canonical renderer for user interfaces is <tt class="docutils literal"><span class="pre">Agg</span></tt> which uses the <a class="reference external" href="http://www.antigrain.com/">Anti-Grain Geometry</a> C++ library to make a raster (pixel) image of the figure. All of the user interfaces except <tt class="docutils literal"><span class="pre">macosx</span></tt> can be used with agg rendering, eg <tt class="docutils literal"><span class="pre">WXAgg</span></tt>, <tt class="docutils literal"><span class="pre">GTKAgg</span></tt>, <tt class="docutils literal"><span class="pre">QT4Agg</span></tt>, <tt class="docutils literal"><span class="pre">TkAgg</span></tt>. In addition, some of the user interfaces support other rendering engines. For example, with GTK, you can also select GDK rendering (backend <tt class="docutils literal"><span class="pre">GTK</span></tt>) or Cairo rendering (backend <tt class="docutils literal"><span class="pre">GTKCairo</span></tt>).</p> <p>For the rendering engines, one can also distinguish between <a class="reference external" href="http://en.wikipedia.org/wiki/Vector_graphics">vector</a> or <a class="reference external" href="http://en.wikipedia.org/wiki/Raster_graphics">raster</a> renderers. Vector graphics languages issue drawing commands like “draw a line from this point to this point” and hence are scale free, and raster backends generate a pixel representation of the line whose accuracy depends on a DPI setting.</p> <p>Here is a summary of the matplotlib renderers (there is an eponymous backed for each; these are <em>non-interactive backends</em>, capable of writing to a file):</p> <table border="1" class="docutils"> <colgroup> <col width="18%" /> <col width="16%" /> <col width="66%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">Renderer</th> <th class="head">Filetypes</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr><td><a class="reference internal" href="../glossary/index.html#term-agg"><em class="xref std std-term">AGG</em></a></td> <td><a class="reference internal" href="../glossary/index.html#term-png"><em class="xref std std-term">png</em></a></td> <td><a class="reference internal" href="../glossary/index.html#term-raster-graphics"><em class="xref std std-term">raster graphics</em></a> – high quality images using the <a class="reference external" href="http://www.antigrain.com/">Anti-Grain Geometry</a> engine</td> </tr> <tr><td>PS</td> <td><a class="reference internal" href="../glossary/index.html#term-ps"><em class="xref std std-term">ps</em></a> <a class="reference internal" href="../glossary/index.html#term-eps"><em class="xref std std-term">eps</em></a></td> <td><a class="reference internal" href="../glossary/index.html#term-vector-graphics"><em class="xref std std-term">vector graphics</em></a> – <a class="reference external" href="http://en.wikipedia.org/wiki/PostScript">Postscript</a> output</td> </tr> <tr><td>PDF</td> <td><a class="reference internal" href="../glossary/index.html#term-pdf"><em class="xref std std-term">pdf</em></a></td> <td><a class="reference internal" href="../glossary/index.html#term-vector-graphics"><em class="xref std std-term">vector graphics</em></a> – <a class="reference external" href="http://en.wikipedia.org/wiki/Portable_Document_Format">Portable Document Format</a></td> </tr> <tr><td>SVG</td> <td><a class="reference internal" href="../glossary/index.html#term-svg"><em class="xref std std-term">svg</em></a></td> <td><a class="reference internal" href="../glossary/index.html#term-vector-graphics"><em class="xref std std-term">vector graphics</em></a> – <a class="reference external" href="http://en.wikipedia.org/wiki/Scalable_Vector_Graphics">Scalable Vector Graphics</a></td> </tr> <tr><td><a class="reference internal" href="../glossary/index.html#term-cairo"><em class="xref std std-term">Cairo</em></a></td> <td><a class="reference internal" href="../glossary/index.html#term-png"><em class="xref std std-term">png</em></a> <a class="reference internal" href="../glossary/index.html#term-ps"><em class="xref std std-term">ps</em></a> <a class="reference internal" href="../glossary/index.html#term-pdf"><em class="xref std std-term">pdf</em></a> <a class="reference internal" href="../glossary/index.html#term-svg"><em class="xref std std-term">svg</em></a> ...</td> <td><a class="reference internal" href="../glossary/index.html#term-vector-graphics"><em class="xref std std-term">vector graphics</em></a> – <a class="reference external" href="http://en.wikipedia.org/wiki/Cairo_(graphics)">Cairo graphics</a></td> </tr> <tr><td><a class="reference internal" href="../glossary/index.html#term-gdk"><em class="xref std std-term">GDK</em></a></td> <td><a class="reference internal" href="../glossary/index.html#term-png"><em class="xref std std-term">png</em></a> <a class="reference internal" href="../glossary/index.html#term-jpg"><em class="xref std std-term">jpg</em></a> <a class="reference internal" href="../glossary/index.html#term-tiff"><em class="xref std std-term">tiff</em></a> ...</td> <td><a class="reference internal" href="../glossary/index.html#term-raster-graphics"><em class="xref std std-term">raster graphics</em></a> – the <a class="reference external" href="http://en.wikipedia.org/wiki/GDK">Gimp Drawing Kit</a></td> </tr> </tbody> </table> <p>And here are the user interfaces and renderer combinations supported; these are <em>interactive backends</em>, capable of displaying to the screen and of using appropriate renderers from the table above to write to a file:</p> <table border="1" class="docutils"> <colgroup> <col width="16%" /> <col width="84%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">Backend</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr><td>GTKAgg</td> <td>Agg rendering to a <a class="reference internal" href="../glossary/index.html#term-gtk"><em class="xref std std-term">GTK</em></a> canvas (requires <a class="reference external" href="http://www.pygtk.org">PyGTK</a>)</td> </tr> <tr><td>GTK</td> <td>GDK rendering to a <a class="reference internal" href="../glossary/index.html#term-gtk"><em class="xref std std-term">GTK</em></a> canvas (not recommended) (requires <a class="reference external" href="http://www.pygtk.org">PyGTK</a>)</td> </tr> <tr><td>GTKCairo</td> <td>Cairo rendering to a <a class="reference internal" href="../glossary/index.html#term-gtk"><em class="xref std std-term">GTK</em></a> Canvas (requires <a class="reference external" href="http://www.pygtk.org">PyGTK</a>)</td> </tr> <tr><td>WXAgg</td> <td>Agg rendering to to a <a class="reference internal" href="../glossary/index.html#term-wxwidgets"><em class="xref std std-term">wxWidgets</em></a> canvas (requires <a class="reference external" href="http://www.wxpython.org/">wxPython</a>)</td> </tr> <tr><td>WX</td> <td>Native <a class="reference internal" href="../glossary/index.html#term-wxwidgets"><em class="xref std std-term">wxWidgets</em></a> drawing to a <a class="reference internal" href="../glossary/index.html#term-wxwidgets"><em class="xref std std-term">wxWidgets</em></a> Canvas (not recommended) (requires <a class="reference external" href="http://www.wxpython.org/">wxPython</a>)</td> </tr> <tr><td>TkAgg</td> <td>Agg rendering to a <a class="reference internal" href="../glossary/index.html#term-tk"><em class="xref std std-term">Tk</em></a> canvas (requires <a class="reference external" href="http://wiki.python.org/moin/TkInter">TkInter</a>)</td> </tr> <tr><td>QtAgg</td> <td>Agg rendering to a <a class="reference internal" href="../glossary/index.html#term-qt"><em class="xref std std-term">Qt</em></a> canvas (requires <a class="reference external" href="http://www.riverbankcomputing.co.uk/software/pyqt/intro">PyQt</a>) (not recommended; use Qt4Agg)</td> </tr> <tr><td>Qt4Agg</td> <td>Agg rendering to a <a class="reference internal" href="../glossary/index.html#term-qt4"><em class="xref std std-term">Qt4</em></a> canvas (requires <a class="reference external" href="http://www.riverbankcomputing.co.uk/software/pyqt/intro">PyQt4</a>)</td> </tr> <tr><td>FLTKAgg</td> <td>Agg rendering to a <a class="reference internal" href="../glossary/index.html#term-fltk"><em class="xref std std-term">FLTK</em></a> canvas (requires <a class="reference external" href="http://pyfltk.sourceforge.net">pyFLTK</a>) (not widely used; consider TKAgg, GTKAgg, WXAgg, or QT4Agg instead)</td> </tr> <tr><td>macosx</td> <td>Cocoa rendering in OSX windows (presently lacks blocking show() behavior when matplotlib is in non-interactive mode)</td> </tr> </tbody> </table> </div> <div class="section" id="what-is-interactive-mode"> <span id="interactive-mode"></span><h2>What is interactive mode?<a class="headerlink" href="#what-is-interactive-mode" title="Permalink to this headline">¶</a></h2> <p>Use of an interactive backend (see <a class="reference internal" href="#what-is-a-backend"><em>What is a backend?</em></a>) permits–but does not by itself require or ensure–plotting to the screen. Whether and when plotting to the screen occurs, and whether a script or shell session continues after a plot is drawn on the screen, depends on the functions and methods that are called, and on a state variable that determines whether matplotlib is in “interactive mode”. The default Boolean value is set by the <tt class="file docutils literal"><span class="pre">matplotlibrc</span></tt> file, and may be customized like any other configuration parameter (see <a class="reference internal" href="../users/customizing.html#customizing-matplotlib"><em>Customizing matplotlib</em></a>). It may also be set via <tt class="xref py py-func docutils literal"><span class="pre">matplotlib.interactive()</span></tt>, and its value may be queried via <tt class="xref py py-func docutils literal"><span class="pre">matplotlib.is_interactive()</span></tt>. Turning interactive mode on and off in the middle of a stream of plotting commands, whether in a script or in a shell, is rarely needed and potentially confusing, so in the following we will assume all plotting is done with interactive mode either on or off.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Major changes related to interactivity, and in particular the role and behavior of <a class="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.show" title="matplotlib.pyplot.show"><tt class="xref py py-func docutils literal"><span class="pre">show()</span></tt></a>, were made in the transition to matplotlib version 1.0, and bugs were fixed in 1.0.1. Here we describe the version 1.0.1 behavior for the primary interactive backends, with the partial exception of <em>macosx</em>.</p> </div> <p>Interactive mode may also be turned on via <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">matplotlib.pyplot.ion()</span></tt></a>, and turned off via <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">matplotlib.pyplot.ioff()</span></tt></a>.</p> <div class="section" id="interactive-example"> <h3>Interactive example<a class="headerlink" href="#interactive-example" title="Permalink to this headline">¶</a></h3> <p>From an ordinary python prompt, or after invoking ipython with no options, try this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span> <span class="n">plt</span><span class="o">.</span><span class="n">ion</span><span class="p">()</span> <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">([</span><span class="mf">1.6</span><span class="p">,</span> <span class="mf">2.7</span><span class="p">])</span> </pre></div> </div> <p>Assuming you are running version 1.0.1 or higher, and you have an interactive backend installed and selected by default, you should see a plot, and your terminal prompt should also be active; you can type additional commands such as:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s">"interactive test"</span><span class="p">)</span> <span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s">"index"</span><span class="p">)</span> </pre></div> </div> <p>and you will see the plot being updated after each line. This is because you are in interactive mode <em>and</em> you are using pyplot functions. Now try an alternative method of modifying the plot. Get a reference to the <a class="reference internal" href="../api/axes_api.html#matplotlib.axes.Axes" title="matplotlib.axes.Axes"><tt class="xref py py-class docutils literal"><span class="pre">Axes</span></tt></a> instance, and call a method of that instance:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">gca</span><span class="p">()</span> <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">([</span><span class="mf">3.1</span><span class="p">,</span> <span class="mf">2.2</span><span class="p">])</span> </pre></div> </div> <p>Nothing changed, because the Axes methods do not include an automatic call to <tt class="xref py py-func docutils literal"><span class="pre">draw_if_interactive()</span></tt>; that call is added by the pyplot functions. If you are using methods, then when you want to update the plot on the screen, you need to call <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>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">plt</span><span class="o">.</span><span class="n">draw</span><span class="p">()</span> </pre></div> </div> <p>Now you should see the new line added to the plot.</p> </div> <div class="section" id="non-interactive-example"> <h3>Non-interactive example<a class="headerlink" href="#non-interactive-example" title="Permalink to this headline">¶</a></h3> <p>Start a fresh session as in the previous example, but now turn interactive mode off:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span> <span class="n">plt</span><span class="o">.</span><span class="n">ioff</span><span class="p">()</span> <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">([</span><span class="mf">1.6</span><span class="p">,</span> <span class="mf">2.7</span><span class="p">])</span> </pre></div> </div> <p>Nothing happened–or at least nothing has shown up on the screen (unless you are using <em>macosx</em> backend, which is anomalous). To make the plot appear, you need to do this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span> </pre></div> </div> <p>Now you see the plot, but your terminal command line is unresponsive; the <tt class="xref py py-func docutils literal"><span class="pre">show()</span></tt> command <em>blocks</em> the input of additional commands until you manually kill the plot window.</p> <p>What good is this–being forced to use a blocking function? Suppose you need a script that plots the contents of a file to the screen. You want to look at that plot, and then end the script. Without some blocking command such as show(), the script would flash up the plot and then end immediately, leaving nothing on the screen.</p> <p>In addition, non-interactive mode delays all drawing until show() is called; this is more efficient than redrawing the plot each time a line in the script adds a new feature.</p> <p>Prior to version 1.0, show() generally could not be called more than once in a single script (although sometimes one could get away with it); for version 1.0.1 and above, this restriction is lifted, so one can write a script like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span> <span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span> <span class="n">plt</span><span class="o">.</span><span class="n">ioff</span><span class="p">()</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span> <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span> <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span> </pre></div> </div> <p>which makes three plots, one at a time.</p> </div> <div class="section" id="summary"> <h3>Summary<a class="headerlink" href="#summary" title="Permalink to this headline">¶</a></h3> <p>In interactive mode, pyplot functions automatically draw to the screen.</p> <p>When plotting interactively, if using object method calls in addition to pyplot functions, then call <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> whenever you want to refresh the plot.</p> <p>Use non-interactive mode in scripts in which you want to generate one or more figures and display them before ending or generating a new set of figures. In that case, use <a class="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.show" title="matplotlib.pyplot.show"><tt class="xref py py-func docutils literal"><span class="pre">show()</span></tt></a> to display the figure(s) and to block execution until you have manually destroyed them.</p> </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="howto_faq.html" title="How-To" >next</a> |</li> <li class="right" > <a href="installing_faq.html" title="Installation" >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" >The Matplotlib FAQ</a> »</li> </ul> </div> <div class="footer"> © Copyright 2008, John Hunter, Darren Dale, Michael Droettboom. Last updated on Oct 04, 2012. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7. </div> </body> </html>