Menu

[r8871]: / trunk / htdocs / backends.html.template  Maximize  Restore  History

Download this file

784 lines (703 with data), 20.1 kB

@header@

<h2>matplotlib backends</h2>

The matplotlib core graphics routines interact with an abstract
renderer and graphics context to allow device independent ouput.
Currently, output to pygtk, wxPython, Tkinter, postscript, pdf, svg,
agg (antigrain geometry) and Cairo are supported.  With not too much
effort, you can port matplotlib to your favorite display.  If you are
interested in porting to one of these platforms, please contact the
<a href="http://sourceforge.net/mailarchive/forum.php?forum_name=matplotlib-devel">matplotlib-devel</a>
mailing list.<p>

<a name="comparision"/><h2>Comparison of backends</h2>

The following table enumerates the capabilities of the different backends.<p>

<!-- This HTML table template is generated by emacs 22.1.1 -->
<table border="1">
  <tr bgcolor="#dddddd">
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      output formats
    </td>
    <td align="left" valign="top">
      rotated text
    </td>
    <td align="left" valign="top">
      mathtext
    </td>
    <td align="left" valign="top">
      usetex
    </td>
    <td align="left" valign="top">
      alpha-blending
    </td>
    <td align="left" valign="top">
      arbitrary clipping<br/> (polar plots)
    </td>
    <td align="left" valign="top">
      images
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
      Agg
    </td>
    <td align="left" valign="top">
      png, raw
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
      Cairo
    </td>
    <td align="left" valign="top">
      pdf,
      png,ps,
      svg,
      svgz
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      X
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
      CocoaAgg
    </td>
    <td align="left" valign="top">
      png,
      raw
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
      Emf
    </td>
    <td align="left" valign="top">
      emf
    </td>
    <td align="left" valign="top">
      ?
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      ?
    </td>
    <td align="left" valign="top">
      ?
    </td>
    <td align="left" valign="top">
      ?
    </td>
    <td align="left" valign="top">
      ?
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
      FltkAgg
    </td>
    <td align="left" valign="top">
      png,
      raw
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
      Gtk
      (Gdk)
    </td>
    <td align="left" valign="top">
      jpeg,
      png,
      raw
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      X
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
      GtkAgg
    </td>
    <td align="left" valign="top">
      jpeg,
      png,
      raw
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
      GtkCairo
    </td>
    <td align="left" valign="top">
      jpeg,
      pdf,
      png,
      ps,
      svg,
      svgz
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      X
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
      Pdf
    </td>
    <td align="left" valign="top">
      pdf
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
      Ps
    </td>
    <td align="left" valign="top">
      eps,ps
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      X
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
      QtAgg
    </td>
    <td align="left" valign="top">
      png,raw
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
      Qt4Agg
    </td>
    <td align="left" valign="top">
      png,raw
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
      Svg
    </td>
    <td align="left" valign="top">
      svg,
      svgz
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      X
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
      TkAgg
    </td>
    <td align="left" valign="top">
      png,raw
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
      Wx
    </td>
    <td align="left" valign="top">
      bmp,
      jpeg,
      pcx,
      png,
      raw,
      tiff,
      xpm
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
    <td align="left" valign="top">
      &nbsp;
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
      WxAgg
    </td>
    <td align="left" valign="top">
      png,raw
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
    <td align="left" valign="top">
      X
    </td>
  </tr>
</table>

<p>

In addition to these factors, there are other factors that may
affect your backend selection:<p>

<ul>
  <li><b>Scalability:</b> For print, the ability to scale the image to any
  resolution is important.  Postscript, Pdf and Svg are good
  candidates for this.  Which will work best for you depends on the
  publication tools you are using.

  <li><b>Speed:</b> When running locally, the Agg-related backends are likely
    to be the fastest since most of Agg is renderered in C extension
    code, and most of the optimization effort has been dedicated to
    it.  However, when running X11 remotely, the Gtk and Wx backends
    may be faster since they send high-level X commands over the
    network rather than pixel data.</li>

  <li><b>Interactivity:</b> All of the GUI backends can be used interactively
    from a python shell (see <a href=interactive.html>interactive</a>) but
    TkAgg is the hands-down winner here since it can be use from any
    python shell whereas the GTK and Wx backends require a GUI specific
    shell.</li>

  <li><b>Pretty widgets:</b> If you want to embed matplotlib in an application and
    pretty GUIs are important to you, Wx, GTK, GTKAgg or GTKCairo are probably
    your best best.  Tk and Fltk widgets are not visually stunning. </li>
</ul>

<h3>Choosing a backend</h3>

You can choose your default backend in
the <a href=matplotlibrc>matplotlibrc</a> file, using the 'backend'
option. The default can be overridden from the command line prompt.
This allows most if not all matplotlib scripts to generate output to
any of the backends without any alterations.  Following the lead of
the matlab <tt>print</tt> command, the backend can be chosen from the
command line with the
<tt>-d</tt> flag, as in

<pre>
# the GUI backends
> python subplot_demo.py -dGTK      # GTK GUI with gdk drawing
> python subplot_demo.py -dGTKAgg   # GTK GUI with antigrain rendering
> python subplot_demo.py -dGTKCairo # GTK GUI with Cairo rendering
> python subplot_demo.py -dTkAgg    # Tkinter GUI with antigrain rendering
> python subplot_demo.py -dWX       # WX backend
> python subplot_demo.py -dWXAgg    # WX GUI with antigrain rendering

# The image backends, no window pops up; you must call savefig
> python subplot_demo.py -dAgg     # antigrain geometry backend image
> python subplot_demo.py -dCairo   # Cairo backend
> python subplot_demo.py -dPS      # postscript backend
> python subplot_demo.py -dPDF     # pdf backend
> python subplot_demo.py -dSVG     # SVG backend
</pre>


For backends that do not have a GUI, no output will be produced unless
a call to <a href=matplotlib.pyplot.html#-savefig>savefig</a> is made.
The recommended way to use the savefig function is to not give an
extension.  The backends will choose the proper extension.  This
allows you to write a single script and select the output format from
the command line.  So a script containing <tt>savefig('somefile')</tt>
will create <tt>somefile.ps</tt> if called with <tt>-dPS</tt> and
<tt>somefile.png</tt> if called with <tt>-dAgg</tt>, and so on.<p>

Alternatively, you can select the backend renderer in your script by
calling the matplotlib <a href=matplotlib.pyplot.html#-use>use</a>
function.  At the top of your script (before you <tt>import
matplotlib.pylab</tt>, just do, for example

<pre>
import matplotlib
matplotlib.use('PS')
</pre>

The current backend strings that are supported are
<a
href=matplotlib.backends.backend_agg.html>Agg</a>, <a
href=matplotlib.backends.backend_cairo.html>Cairo</a>, <a
href=matplotlib.backends.backend_fltkagg.html>FltkAgg</a>, <a
href=matplotlib.backends.backend_gtk.html>GTK</a>, <a
href=matplotlib.backends.backend_gtkagg.html>GTKAgg</a>, <a
href=matplotlib.backends.backend_gtkcairo.html>GTKCairo</a>, <a
href=matplotlib.backends.backend_ps.html>PS</a>, <a
href=matplotlib.backends.backend_pdf.html>PDF</a>, <a
href=matplotlib.backends.backend_svg.html>SVG</a>, <a
href=matplotlib.backends.backend_tkagg.html>TkAgg</a>, <a
href=matplotlib.backends.backend_wx.html>WX</a>, <a
href=matplotlib.backends.backend_wxagg.html>WXAgg</a>, <a
href=matplotlib.backends.backend_template.html>Template</a>.

The default is GTKAgg.  Template is a do nothing backend that serves as a
template for backend writers (volunteers welcome!).<p>

To get the most of your backend of choice, you may need to set an
environment variable controlling the fonts; see <a
href="fonts.html">fonts</a> for more information.<p>

<h2>Backend requirements</h2>

Each of the backends have a different set of requirements, listed
below.  All require <a href=http://numpy.scipy.org/>numpy</a>
modules.

Specific information for installing the other backends can be found on
this page below or by clicking
<ul>
<li><a href=#Agg>Agg</a></li>
<li><a href=#Cairo>Cairo</a></li>
<li><a href=#TkAgg>TkAgg</a></li>
<li><a href=#GTK>GTK</a></li>
<li><a href=#GTKAgg>GTKAgg</a></li>
<li><a href=#GTKCairo>GTKCairo</a></li>
<li><a href=#WX>WX</a></li>
<li><a href=#WXAgg>WXAgg</a></li>
<li><a href=#PS>PS</a></li>
<li><a href=#PDF>PDF</a></li>
<li><a href=#SVG>SVG</a></li>
</ul>


<a name=Agg><h3>Agg</h3></a>

The <a href=http://antigrain.com>antigrain geometry</a> library is a
platform independent library that provides efficient antialiased
rendering with a vector model.  Because it doesn't depend on a GUI
framework, it is suitable for use in web application servers
generating images for html inclusion or for generating images in batch
scripts.  Agg can also be embedded in GUI applications, as in the
GTKAgg and TkAgg backends -- see <tt>setup.py</tt><p>

matplotlib includes its own copy of Agg.  It does not need to be
installed separately, even if compiling matplotlib from source.

<a name=Cairo><h3>Cairo</h3></a>

<a href=http://cairographics.org>Cairo</a> is a vector graphics
library designed to provide high-quality display and print output.
It doesn't depend on a GUI framework and is suitable for use in web
application servers generating images for html inclusion or for generating
images in batch scripts.  Cairo can also be embedded in GUI applications, as in
the GTKCairo backend<p>

It requires pycairo version 0.5.0 or higher from
<a href=http://www.freedesktop.org/Cairo/download>Cairo downloads</a>


<a name=GTK><h3>GTK</h3></a>

This was the first backend supported by matplotlib; it requires

<ul>

  <li><a href=http://numpy.scipy.org/>numpy</a></li>

  <li><a href=http://prdownloads.sourceforge.net/gtk-win>
GTK runtime</a></li>

  <li><a href=http://www.pygtk.org>pygtk</a>
  version 2.0.0 or later</li>

</ul>

<h2> Windows GTK Quickstart</h2>

These install instructions will provide the <a
href=backends.html#GTK>GTK<a/> and <a
href=backends.html#GTKAgg>GTKAgg<a/> backends on windows.  If
you encounter troubles, please see <a
href=http://matplotlib.sourceforge.net/faq.html#GTKPATH>the
FAQ</a><ul>

<li>Install <a href=http://numpy.scipy.org/>numpy</a>. If you are using the enthought edition of
python, you can skip this step.</li>

<li>Install the <a href=http://prdownloads.sourceforge.net/gtk-win>
GTK runtime</a> (currently version 2.2.4.1). <i>I recommend not using
the default install location as it makes it difficult to set your
path; use C:\GTk instead</i></li>

<li>Install <a
href=http://www.pcpm.ucl.ac.be/~gustin/win32_ports/binaries/pygtk-2.2.0-1.win32-py2.3.exe>pygtk-2.2.0</a></li>

<li>Modify your path.  Check <a
href=http://www.async.com.br/faq/pygtk/index.py?req=show&file=faq21.012.htp>this
FAQ entry</a> for information on how to do this right.

<li> Install matplotlib using the windows installer on the <a
ref=http://sourceforge.net/projects/matplotlib>download page<a></li>

</ul>


<a name=WX><h3>WX</h3></a>

Requires <a href=http://www.wxpython.org>wxpython</a>.  Windows users
may want to consider the <a
href=http://www.enthought.com/python>enthought edition</a> of python,
which comes with wxpython and numpy built in, so matplotlib will
work right out of the box.  matplotlib under WX has been tested on
linux, win32 and OSX.<p>

<a name=WXAgg><h3>WXAgg</h3></a>

Requires <a href=http://www.wxpython.org>wxpython</a>.  Windows users
may want to consider the <a
href=http://www.enthought.com/python>enthought edition</a> of python,
which comes with wxpython and numpy built in, so matplotlib will
work right out of the box.  matplotlib under WXAgg has been tested on
linux, win32 and OSX.<p>

<a name=GTKAgg><h3>GTKAgg: GTK GUI with antigrain rendering</h3></a>

GTK has a very nice and portable widget set.  The gdk drawing
commands, while fast, are somewhat limited.  To overcome this
limitation, GTKAgg marries the widgets of GTK with the rendering of <a
href=http:///antigrain.com>antigrain</a>.  You need the <a
href=backends.html#Agg>agg</a> and <a href=backends.html#GTK>gtk</a>
prerequisites.  Agg is builtin, so you only need to
install pygtk and the GTK runtime, as described in <a
href=backends.html#GTK>gtk</a>.


<a name=GTKAgg><h3>GTKCairo</h3></a>

GTKCairo uses Cairo rendering from within the GTK GUI.
You need the <a href=backends.html#Cairo>Cairo</a> and <a href=backends.html#GTK>gtk</a>
prerequisites.


<a name=TkAgg><h3>Tkinter GUI backend</h3></a>

Todd Miller has written a backend for Tkinter that uses the agg
backend for rendering.  To use Tkagg, you need to set the BUILD_TKAGG
flag in setup.py.  The windows installer comes with TkAgg prebuilt.
See <a href=backends.html#Agg>agg backend</a> for more information on
agg rendering and fonts.  NOTE: on at least some versions of redhat
linux, you must install a separate tkinter package, apparently
tkinter-2.2.2-26.i386.rpm on Red Hat 9 linux.  Alternately, Python
built from source code (Python.org tarball, not the redhat SRPM)
includes Tkinter support by default on Red Hat Linux.


In general, TkAgg is known to work with
<ul>
<li>python</li>
<li>idle -n (set <tt>tk.PYTHONINSPECT : True</tt> in <a href=matplotlibrc>matplotlibrc</a> </li>
<li>IPython</li>
</ul>

TkAgg is known not work with:

<ul>
<li>SciTE</li>
<li>pythonw</li>
<li>Pythonwin</li>
<li>idle</li>
</ul>

Both of the latter shells fail with a RuntimeError "abnormal program
termination".

I checked on www.python.org about Tkinter and Pythonwin and they're
known not to work together so that explains TkAgg on Pythonwin.  <a
href=http://www.python.org/topics/tkinter/trouble.html>tkinter
trouble</a>.<p>

I also looked into SciTE a little and discovered that it is related to
Scintilla which in turn was derived from Pythonwin.  <a
href=http://www.scintilla.org>scintilla</a> This indicates to me that
the same problem with Tkinter may be affecting both (SciTE and
Pythonwin)... but I am out on a limb.<p>


<a name=PS><h3>Postscript</h3></a>

The only requirement is <a href=http://numpy.scipy.org/>numpy</a>.<p>

See the <a href=fonts.html#PSFONTS>fonts page</a> for more information
about getting the postscript backend setup for proper font rendering;
notably, you must make sure there are some '*.afm' files in your
<tt>AFMPATH</tt> if you want to use fonts other than the ones that
ship with matplotlib.

<a name=PS><h3>PDF</h3></a>

The only requirement is <a href=http://numpy.scipy.org/>numpy</a>.<p>

<a name=PS><h3>SVG</h3></a>

The only requirement is <a href=http://numpy.scipy.org/>numpy</a>.<p>


@footer@


Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.