-
Book Overview & Buying
-
Table Of Contents
Matplotlib for Python Developers
By :
The aim of Matplotlib is to generate graphs. So, we need a way to actually view these images or even to save them to files. We're going to look at the various output formats available in Matplotlib and the graphical user interfaces (GUIs) supported by the library.
Given its scientific roots (that means several different needs), Matplotlib has a lot of output formats available, which can be used for articles/books and other print publications, for web pages, or for any other reason we can think of. Let's first differentiate the output formats into two distinct categories:
Of course, Matplotlib supports both the categories, particularly with the following output formats:
|
Format |
Type |
Description |
|---|---|---|
|
EPS |
Vector |
Encapsulated PostScript. |
|
JPG |
Raster |
Graphic format with lossy compression method for photographic output. |
|
|
Vector |
Portable Document Format (PDF). |
|
PNG |
Raster |
Portable Network Graphics (PNG), a raster graphics format with a lossless compression method (more adaptable to line art than JPG). |
|
PS |
Vector |
Language widely used in publishing and as printers jobs format. |
|
SVG |
Vector |
Scalable Vector Graphics (SVG), XML based. |
PS or EPS formats are particularly useful for plots inclusion in LaTeX documents, the main scientific articles format since decades.
In the previous section, we saw the file output formats — they are also called hardcopy backends as they create something (a file on disk).
A backend that displays the image on screen is called a user interface backend.
The backend is that part of Matplotlib that works behind the scenes and allows the software to target several different output formats and GUI libraries (for screen visualization).
In order to be even more flexible, Matplotlib introduces the following two layers structured (only for GUI output):
The standard renderer is the Anti-Grain Geometry ( AGG) library, a high performance rendering engine which is able to create images of publication level quality, with anti-aliasing, and subpixel accuracy. AGG is responsible for the beautiful appearance of Matplotlib graphs.
The canvas is provided with the GUI libraries, and any of them can use the AGG rendering, along with the support for other rendering engines (for example, GTK+).
Let's have a look at the user interface toolkits and their available renderers:
|
Backend |
Description |
|---|---|
|
|
GTK+ (The GIMP ToolKit GUI library) canvas with AGG rendering. |
|
|
GTK+ canvas with GDK rendering. GDK rendering is rather primitive, and doesn't include anti-aliasing for the smoothing of lines. |
|
|
GTK+ canvas with Cairo rendering. |
|
|
wxWidgets (cross-platform GUI and tools library for GTK+, Windows, and Mac OS X. It uses native widgets for each operating system, so applications will have the look and feel that users expect on that operating system) canvas with AGG rendering. |
|
|
wxWidgets canvas with native wxWidgets rendering. |
|
|
Tk (graphical user interface for Tcl and many other dynamic languages) canvas with AGG rendering. |
|
|
Qt (cross-platform application framework for desktop and embedded development) canvas with AGG rendering (for Qt version 3 and earlier). |
|
|
Qt4 canvas with AGG rendering. |
|
|
FLTK (cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and Mac OS X) canvas with Agg rendering. |
Here is the list of renderers for file output:
|
Renderer |
File type |
|---|---|
|
AGG |
|
|
PS |
|
|
|
|
|
SVG |
.svg |
|
Cairo |
|
|
GDK |
|
The renderers mentioned in the previous table can be used directly in Matplotlib, when we want only to save the resulting graph into a file (without any visualization of it), in any of the formats supported.
We have to pay attention when choosing which backend to use. For example, if we don't have a graphical environment available, then we have to use the AGG backend (or any other file). If we have installed only the GTK+ Python bindings, then we can't use the WX backend.
Change the font size
Change margin width
Change background colour