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:
Raster images: These are the classic images we can find on the Web or used for pictures. The most well known raster file formats are PNG, JPG, and BMP. They are widespread and well supported. The format of these images is like a matrix, with rows and columns, and at every matrix cell we have a pixel description (containing information such as colors). This format is said to be resolution-dependent, because the size of the matrix (the number of rows and columns) is determined when the image is created. An important parameter for raster images is the DPI(dots-per-inch) value. Once the image dimensions are decided (length and width, in inches), the DPI value specifies the detail level of the image. Hence, higher the DPI value, higher is the quality of the image (because for the same inch we get more dots). Scaling operations such as zooming or resizing can result in a loss of quality, because the image contains only a limited amount of information.
Vector images: As opposed to raster images, vector images contain a description of the image in the form of mathematical equations and geometrical primitives (for example, points, lines, curves, polygons, or shapes). We can think of this format as a series of directives to plot the image: "Draw a point here, draw another point there, draw a line between those two points" and so on. Given this descriptive format, these images are said to be resolution-independent, because it's the image interpreter that replots the image at the requested resolution using the instructions in it. Typical examples of vector image usage are typesetting and CAD (architectural or mechanical parts drawings).
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.