Book Image

Matplotlib for Python Developers

Book Image

Matplotlib for Python Developers

Overview of this book

Providing appealing plots and graphs is an essential part of various fields such as scientific research, data analysis, and so on. Matplotlib, the Python 2D plotting library, is used to produce publication-quality figures in a variety of hardcopy formats and interactive environments across platforms. This book explains creating various plots, histograms, power spectra, bar charts, error charts, scatter-plots and much more using the powerful Matplotlib library to get impressive out-of-the-box results. This book gives you a comprehensive tour of the key features of the Matplotlib Python 2D plotting library, right from the simplest concepts to the most advanced topics. You will discover how easy it is to produce professional-quality plots when you have this book to hand. The book introduces the library in steps. First come the basics: introducing what the library is, its important prerequisites (and terminology), installing and configuring Matplotlib, and going through simple plots such as lines, grids, axes, and charts. Then we start with some introductory examples, and move ahead by discussing the various programming styles that Matplotlib allows, and several key features. Further, the book presents an important section on embedding applications. You will be introduced to three of the best known GUI libraries 'GTK+, Qt, and wxWidgets' and presented with the steps to implement to include Matplotlib in an application written using each of them. You will learn through an incremental approach: from a simple example that presents the peculiarities of the GUI library, to more complex ones, using GUI designer tools. Because the Web permeates all of our activities, a part of the book is dedicated to showing how Matplotlib can be used in a web environment, and another section focuses on using Matplotlib with common Python web frameworks, namely, Pylons and Django. Last, but not least, you will go through real-world examples, where you will see some real situations in which you can use Matplotlib.
Table of Contents (14 chapters)
Matplotlib for Python Developers
Credits
About the Author
About the Reviewers
Preface

Output formats and backends


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.

Output formats

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.

PDF

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.

Backends

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 renderer: This actually does the drawing

  • The canvas: This is the destination of the figure

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

GTKAgg

GTK+ (The GIMP ToolKit GUI library) canvas with AGG rendering.

GTK

GTK+ canvas with GDK rendering. GDK rendering is rather primitive, and doesn't include anti-aliasing for the smoothing of lines.

GTKCairo

GTK+ canvas with Cairo rendering.

WxAgg

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.

WX

wxWidgets canvas with native wxWidgets rendering.

TkAgg

Tk (graphical user interface for Tcl and many other dynamic languages) canvas with AGG rendering.

QtAgg

Qt (cross-platform application framework for desktop and embedded development) canvas with AGG rendering (for Qt version 3 and earlier).

Qt4Agg

Qt4 canvas with AGG rendering.

FLTKAgg

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

.png

PS

.eps or .ps

PDF

.pdf

SVG

.svg

Cairo

.png, .ps, .pdf, .svg

GDK

.png, .jpg

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.