@@ -146,4 +146,147 @@ complicated applications, this explicitness and clarity becomes
146
146
increasingly valuable, and the richer and more complete object-oriented
147
147
interface will likely make the program easier to write and maintain.
148
148
149
+ Backends
150
+ ========
151
+
152
+ .. _what-is-a-backend :
153
+
154
+ What is a backend?
155
+ ------------------
156
+
157
+ A lot of documentation on the website and in the mailing lists refers
158
+ to the "backend" and many new users are confused by this term.
159
+ matplotlib targets many different use cases and output formats. Some
160
+ people use matplotlib interactively from the python shell and have
161
+ plotting windows pop up when they type commands. Some people embed
162
+ matplotlib into graphical user interfaces like wxpython or pygtk to
163
+ build rich applications. Others use matplotlib in batch scripts to
164
+ generate postscript images from some numerical simulations, and still
165
+ others in web application servers to dynamically serve up graphs.
166
+
167
+ To support all of these use cases, matplotlib can target different
168
+ outputs, and each of these capabilities is called a backend; the
169
+ "frontend" is the user facing code, ie the plotting code, whereas the
170
+ "backend" does all the hard work behind-the-scenes to make the
171
+ figure. There are two types of backends: user interface backends (for
172
+ use in pygtk, wxpython, tkinter, qt, macosx, or fltk; also
173
+ referred to as "interactive backends") and hardcopy backends to
174
+ make image files (PNG, SVG, PDF, PS; also referred to as "non-interactive
175
+ backends").
176
+
177
+ There are a two primary ways to configure your backend. One is to set
178
+ the ``backend `` parameter in your ``matplotlibrc `` file (see
179
+ :ref: `customizing-matplotlib `)::
180
+
181
+ backend : WXAgg # use wxpython with antigrain (agg) rendering
182
+
183
+ The other is to use the matplotlib :func: `~matplotlib.use ` directive::
184
+
185
+ import matplotlib
186
+ matplotlib.use('PS') # generate postscript output by default
187
+
188
+ If you use the ``use `` directive, this must be done before importing
189
+ :mod: `matplotlib.pyplot ` or :mod: `matplotlib.pylab `.
190
+
191
+ .. note ::
192
+ Backend name specifications are not case-sensitive; e.g., 'GTKAgg'
193
+ and 'gtkagg' are equivalent.
194
+
195
+ With a typical installation of matplotlib, such as from a
196
+ binary installer or a linux distribution package, a good default
197
+ backend will already be set, allowing both interactive work and
198
+ plotting from scripts, with output to the screen and/or to
199
+ a file, so at least initially you will not need to use either of the
200
+ two methods given above.
201
+
202
+ If, however, you want to write graphical user interfaces, or a web
203
+ application server (:ref: `howto-webapp `), or need a better
204
+ understanding of what is going on, read on. To make things a little
205
+ more customizable for graphical user interfaces, matplotlib separates
206
+ the concept of the renderer (the thing that actually does the drawing)
207
+ from the canvas (the place where the drawing goes). The canonical
208
+ renderer for user interfaces is ``Agg `` which uses the `Anti-Grain
209
+ Geometry `_ C++ library to make a raster (pixel) image of the figure.
210
+ All of the user interfaces except ``macosx `` can be used with
211
+ agg rendering, eg
212
+ ``WXAgg ``, ``GTKAgg ``, ``QT4Agg ``, ``TkAgg ``. In
213
+ addition, some of the user interfaces support other rendering engines.
214
+ For example, with GTK, you can also select GDK rendering (backend
215
+ ``GTK ``) or Cairo rendering (backend ``GTKCairo ``).
216
+
217
+ For the rendering engines, one can also distinguish between `vector
218
+ <http://en.wikipedia.org/wiki/Vector_graphics> `_ or `raster
219
+ <http://en.wikipedia.org/wiki/Raster_graphics> `_ renderers. Vector
220
+ graphics languages issue drawing commands like "draw a line from this
221
+ point to this point" and hence are scale free, and raster backends
222
+ generate a pixel representation of the line whose accuracy depends on a
223
+ DPI setting.
224
+
225
+ Here is a summary of the matplotlib renderers (there is an eponymous
226
+ backed for each; these are *non-interactive backends *, capable of
227
+ writing to a file):
228
+
229
+ ============= ============ ================================================
230
+ Renderer Filetypes Description
231
+ ============= ============ ================================================
232
+ :term: `AGG ` :term: `png ` :term: `raster graphics ` -- high quality images
233
+ using the `Anti-Grain Geometry `_ engine
234
+ PS :term: `ps ` :term: `vector graphics ` -- Postscript _ output
235
+ :term: `eps `
236
+ PDF :term: `pdf ` :term: `vector graphics ` --
237
+ `Portable Document Format `_
238
+ SVG :term: `svg ` :term: `vector graphics ` --
239
+ `Scalable Vector Graphics `_
240
+ :term: `Cairo ` :term: `png ` :term: `vector graphics ` --
241
+ :term: `ps ` `Cairo graphics `_
242
+ :term: `pdf `
243
+ :term: `svg `
244
+ ...
245
+ :term: `GDK ` :term: `png ` :term: `raster graphics ` --
246
+ :term: `jpg ` the `Gimp Drawing Kit `_
247
+ :term: `tiff `
248
+ ...
249
+ ============= ============ ================================================
250
+
251
+ And here are the user interfaces and renderer combinations supported;
252
+ these are *interactive backends *, capable of displaying to the screen
253
+ and of using appropriate renderers from the table above to write to
254
+ a file:
255
+
256
+ ============ ================================================================
257
+ Backend Description
258
+ ============ ================================================================
259
+ GTKAgg Agg rendering to a :term: `GTK ` canvas (requires PyGTK _)
260
+ GTK GDK rendering to a :term: `GTK ` canvas (not recommended)
261
+ (requires PyGTK _)
262
+ GTKCairo Cairo rendering to a :term: `GTK ` Canvas (requires PyGTK _)
263
+ WXAgg Agg rendering to to a :term: `wxWidgets ` canvas
264
+ (requires wxPython _)
265
+ WX Native :term: `wxWidgets ` drawing to a :term: `wxWidgets ` Canvas
266
+ (not recommended) (requires wxPython _)
267
+ TkAgg Agg rendering to a :term: `Tk ` canvas (requires TkInter _)
268
+ QtAgg Agg rendering to a :term: `Qt ` canvas (requires PyQt _)
269
+ (not recommended; use Qt4Agg)
270
+ Qt4Agg Agg rendering to a :term: `Qt4 ` canvas (requires PyQt4 _)
271
+ FLTKAgg Agg rendering to a :term: `FLTK ` canvas (requires pyFLTK _)
272
+ (not widely used; consider TKAgg, GTKAgg, WXAgg, or
273
+ QT4Agg instead)
274
+ macosx Cocoa rendering in OSX windows
275
+ (presently lacks blocking show() behavior when matplotlib
276
+ is in non-interactive mode)
277
+ ============ ================================================================
278
+
279
+ .. _`Anti-Grain Geometry` : http://www.antigrain.com/
280
+ .. _Postscript : http://en.wikipedia.org/wiki/PostScript
281
+ .. _`Portable Document Format` : http://en.wikipedia.org/wiki/Portable_Document_Format
282
+ .. _`Scalable Vector Graphics` : http://en.wikipedia.org/wiki/Scalable_Vector_Graphics
283
+ .. _`Cairo graphics` : http://en.wikipedia.org/wiki/Cairo_(graphics)
284
+ .. _`Gimp Drawing Kit` : http://en.wikipedia.org/wiki/GDK
285
+ .. _PyGTK : http://www.pygtk.org
286
+ .. _wxPython : http://www.wxpython.org/
287
+ .. _TkInter : http://wiki.python.org/moin/TkInter
288
+ .. _PyQt : http://www.riverbankcomputing.co.uk/software/pyqt/intro
289
+ .. _PyQt4 : http://www.riverbankcomputing.co.uk/software/pyqt/intro
290
+ .. _pyFLTK : http://pyfltk.sourceforge.net
291
+
149
292
0 commit comments