Reader small image

You're reading from  Python Data Visualization Cookbook

Product typeBook
Published inNov 2013
Reading LevelIntermediate
PublisherPackt
ISBN-139781782163367
Edition1st Edition
Languages
Right arrow
Author (1)
Igor Milovanovic
Igor Milovanovic
author image
Igor Milovanovic

Igor Milovanović is an experienced developer, with strong background in Linux system knowledge and software engineering education. He is skilled in building scalable data-driven distributed software rich systems. An evangelist for high-quality systems design, he has a strong interest in software architecture and development methodologies. Igor is always committed to advocating methodologies that promote high-quality software, such as test-driven development, one-step builds, and continuous integration. He also possesses solid knowledge of product development. With field experience and official training, he is capable of transferring knowledge and communication flow from business to developers and vice versa. Igor is most grateful to his girlfriend for letting him spend hours on work instead with her and being an avid listener to his endless book monologues. He thanks his brother for being the strongest supporter. He is also thankful to his parents for letting him develop in various ways to become a person he is today.
Read more about Igor Milovanovic

Right arrow

Chapter 8. More on matplotlib Gems

In this chapter we will cover:

  • Drawing barbs

  • Making a box and whisker plot

  • Making Gantt charts

  • Making errorbars

  • Making use of text and font properties

  • Rendering text with LaTeX

  • Understanding the difference between pyplot and OO API

Introduction


In this chapter we will explore some less frequently used features of the matplotlib package. Some of these examples stretch the matplotlib original target, but they show what can be done with a little creativity, and prove that matplolib is full featured and generically oriented.

Drawing barbs


A barb is a representation of the speed and direction of wind, and is mainly deployed by meteorology scientists. In theory they can be used to visualize any type of two-dimensional vector quantities. They are similar to arrows (quivers), but the difference is that arrows represent vector magnitude by the length of the arrow, while barbs give more information about the vector's magnitude by employing lines or triangles as increments of magnitude.

We will explain what barbs are, how to read them, and how to visualize them using Python and matplotlib. Here's a typical set of barbs:

In the preceding diagram, the triangle, also known as flag, represents the largest increment. A full line or barb, represents a smaller increment; a half line is the smallest increment.

The increments are in the order of 5, 10, and 65 for a half-line, line, and triangle respectively. The values here represent, for meteorologists at least, wind speed in nautical miles per hour (knots).

We ordered the barbs...

Making a box and a whisker plot


Do you want to visualize a series of data measurement (or observations) to show several properties of the data series (such as the median value, the spread of the data, and the distribution of the data) in one plot? And would you want to do that in a way where you can visually compare several similar data series? How would you visualize them? Welcome to the box-and-whisker plot! Probably the best plot type for comparing distributions, if you are talking to people used to information density.

The box-and-whisker plot usage examples range from comparing test scores between schools to comparing process parameters before and after changes (optimization).

Getting ready

What are the elements of box and whisker plots? As we see in the following diagram, we have several important elements that carry information in the box-and-whisker plot. The first component is the box that carries information about the interquartile range going from lower to upper quartile values....

Making Gantt charts


One form of very widely-used visualization of time-based data is a Gantt chart. Named after the mechanical engineer Henry Gantt who invented it in 1910s, it is almost exclusively used to visualize work breakdown structures in project management. This chart is loved by managers for its descriptive value and not so loved by employees, especially when the project deadline is near.

Because it is very common, almost every one can understand and read it, even if it is overloaded with additional (related and unrelated) information.

A basic Gantt chart has a time series on the x axis, and a set of labels that represent tasks or subtasks on the y axis. Task duration is usually visualized either as a line or as a bar chart, extending from the start to end time of a given task.

If subtasks are present, one or many subtasks have a parent task, in which the case total time of a task is aggregated from subtasks in such a way that overlapping and gap time is accounted for. This is useful...

Making errorbars


Error bars are useful to display the dispersion of data on a plot. They are relatively simple as a form of visualization; however, they are also a bit problematic because what is shown as an error varies across different sciences and publications. This does not lessen the usefulness of error bars, it just imposes the need to always be careful and explicitly state the nature of the error visualized as an error bar.

Getting ready

To be able to plot an error bar in the raw observed data, we need to compute the mean and the error we want to display.

The error we compute represents the 95 percent confidence interval that the mean we get from our observation is stable, which means our observations are good estimates of the whole population.

matplotlib supports these type of plots via matplotlib.pyplot.errorbar function.

It offers several capabilities around error bars. They can be vertical (yerr) or horizontal (xerr), and symmetrical or asymmetrical.

How to do it...

In the following...

Making use of text and font properties


We already learned how to annotate the plot by adding legends, but sometimes we want more with text. This recipe will explain and demonstrate more features of text manipulation in matplotlib, giving a powerful toolkit for even advanced typesetting needs.

We will not cover LaTeX support in this recipe, as there is a recipe named Rendering text with LaTeX in this chapter.

Getting ready

We start with listing of the most useful set of functions that matplotlib offers. Most of the functions are available via pyplot module's interface, but we map their origin function here to allow you to explore more if a particular text feature is not covered in this recipe.

Basic text manipulations and their mapping in matplotlib OO API is presented in the following table:

Rendering text with LaTeX


If we want to plot more scientific graphics and explain math as it should be using scientific notations and complex equations on the figures, we need support from the best.

Although matplotlib has support for math text rendering, the best support comes from the LaTeX community, proven in the task being used for many decades.

LaTeX is a high-quality typesetting system for the production of scientific and technical documentation, being a de facto standard for scientific typesetting or publication. It is a free software, available on the majority of desktop platforms used today as prepackages binary installation; hence, it is easy to install.

The basic syntax of LaTeX is similar to markup languages; so to produce satisfactory content, one would write focusing more on the structure than on the look and style. For example:

\documentclass{article}
\title{This here is a title of my document}
\author{Peter J. S. Smith}
\date{September 2013}
\begin{document}
   \maketitle
 ...

Understanding the difference between pyplot and OO API


This recipe will try to explain some of the programming interfaces in matplotlib and make a comparison of pyplot and object-oriented API (Application Programming Interface). Depending on the task at hand, this will allow us to decide why and when to use either of those interfaces.

Getting ready

When the matplotlib library started, it was similar to many open source projects—there was no proper (free) solution to the problem a person had, so he wrote one. The problem encountered with MATLAB® was with performance for the task in hand (http://www.aosabook.org/en/matplotlib.html), and the original author already had knowledge of both MATLAB® and Python, so he started writing matplotlib as a solution for his need for the current project.

This is the main reason matplotlib has a MATLAB®-like interface that allows one to quickly plot data without worrying about background details, which platform matplotlib is running on, which are the underlying...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Python Data Visualization Cookbook
Published in: Nov 2013Publisher: PacktISBN-13: 9781782163367
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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Igor Milovanovic

Igor Milovanović is an experienced developer, with strong background in Linux system knowledge and software engineering education. He is skilled in building scalable data-driven distributed software rich systems. An evangelist for high-quality systems design, he has a strong interest in software architecture and development methodologies. Igor is always committed to advocating methodologies that promote high-quality software, such as test-driven development, one-step builds, and continuous integration. He also possesses solid knowledge of product development. With field experience and official training, he is capable of transferring knowledge and communication flow from business to developers and vice versa. Igor is most grateful to his girlfriend for letting him spend hours on work instead with her and being an avid listener to his endless book monologues. He thanks his brother for being the strongest supporter. He is also thankful to his parents for letting him develop in various ways to become a person he is today.
Read more about Igor Milovanovic

matplotlib.pyplot

Matplotlib API

Description

text

matplotlib.axes.Axes.text

Adds text to the axes at the location specified by (x, y). Argument fontdict allows us to override generic font properties...