Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Events
Videos
Audiobooks
Packt Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Matplotlib for Python Developers

You're reading from   Matplotlib for Python Developers Python developers who want to learn Matplotlib need look no further. This book covers it all with a practical approach including lots of code and images. Take this chance to learn 2D plotting through real-world examples.

Arrow left icon
Product type Paperback
Published in Nov 2009
Publisher Packt
ISBN-13 9781847197900
Length 308 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Sandro Tosi Sandro Tosi
Author Profile Icon Sandro Tosi
Sandro Tosi
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Introduction to Matplotlib FREE CHAPTER 2. Getting Started with Matplotlib 3. Decorate Graphs with Plot Styles and Types 4. Advanced Matplotlib 5. Embedding Matplotlib in GTK+ 6. Embedding Matplotlib in Qt 4 7. Embedding Matplotlib in wxWidgets 8. Matplotlib for the Web 9. Matplotlib in the Real World Index

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.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Matplotlib for Python Developers
You have been reading a chapter from
Matplotlib for Python Developers
Published in: Nov 2009
Publisher: Packt
ISBN-13: 9781847197900
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Modal Close icon
Modal Close icon