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
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
matplotlib Plotting Cookbook
matplotlib Plotting Cookbook

matplotlib Plotting Cookbook: Discover how easy it can be to create great scientific visualizations with Python. This cookbook includes over sixty matplotlib recipes together with clarifying explanations to ensure you can produce plots of high quality.

eBook
$9.99 $28.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

matplotlib Plotting Cookbook

Chapter 1. First Steps

In this chapter, we will cover:

  • Installing matplotlib

  • Plotting one curve

  • Using NumPy

  • Plotting multiple curves

  • Plotting curves from file data

  • Plotting points

  • Plotting bar charts

  • Plotting multiple bar charts

  • Plotting stacked bar charts

  • Plotting back-to-back bar charts

  • Plotting pie charts

  • Plotting histograms

  • Plotting boxplots

  • Plotting triangulations

Introduction


matplotlib makes scientific plotting very straightforward. matplotlib is not the first attempt at making the plotting of graphs easy. What matplotlib brings is a modern solution to the balance between ease of use and power. matplotlib is a module for Python, a programming language. In this chapter, we will provide a quick overview of what using matplotlib feels like. Minimalistic recipes are used to introduce the principles matplotlib is built upon.

Installing matplotlib


Before experimenting with matplotlib, you need to install it. Here we introduce some tips to get matplotlib up and running without too much trouble.

How to do it...

We have three likely scenarios: you might be using Linux, OS X, or Windows.

Linux

Most Linux distributions have Python installed by default, and provide matplotlib in their standard package list. So all you have to do is use the package manager of your distribution to install matplotlib automatically. In addition to matplotlib, we highly recommend that you install NumPy, SciPy, and SymPy, as they are supposed to work together. The following list consists of commands to enable the default packages available in different versions of Linux:

  • Ubuntu: The default Python packages are compiled for Python 2.7. In a command terminal, enter the following command:

    sudo apt-get install python-matplotlib python-numpy python-scipy python-sympy
    
  • ArchLinux: The default Python packages are compiled for Python 3. In a command terminal, enter the following command:

    sudo pacman -S python-matplotlib python-numpy python-scipy python-sympy
    

    If you prefer using Python 2.7, replace python by python2 in the package names

  • Fedora: The default Python packages are compiled for Python 2.7. In a command terminal, enter the following command:

    sudo yum install python-matplotlib numpy scipy sympy
    

    Note

    There are other ways to install these packages; in this chapter, we propose the most simple and seamless ways to do it.

Windows and OS X

Windows and OS X do not have a standard package system for software installation. We have two options—using a ready-made self-installing package or compiling matplotlib from the code source. The second option involves much more work; it is worth the effort to have the latest, bleeding edge version of matplotlib installed. Therefore, in most cases, using a ready-made package is a more pragmatic choice.

You have several choices for ready-made packages: Anaconda, Enthought Canopy, Algorete Loopy, and more! All these packages provide Python, SciPy, NumPy, matplotlib, and more (a text editor and fancy interactive shells) in one go. Indeed, all these systems install their own package manager and from there you install/uninstall additional packages as you would do on a typical Linux distribution. For the sake of brevity, we will provide instructions only for Enthought Canopy. All the other systems have extensive documentation online, so installing them should not be too much of a problem.

So, let's install Enthought Canopy by performing the following steps:

  1. Download the Enthought Canopy installer from https://www.enthought.com/products/canopy. You can choose the free Express edition. The website can guess your operating system and propose the right installer for you.

  2. Run the Enthought Canopy installer. You do not need to be an administrator to install the package if you do not want to share the installed software with other users.

  3. When installing, just click on Next to keep the defaults. You can find additional information about the installation process at http://docs.enthought.com/canopy/quick-start.html.

That's it! You will have Python 2.7, NumPy, SciPy, and matplotlib installed and ready to run.

Plotting one curve


The initial example of Hello World! for a plotting software is often about showing a simple curve. We will keep up with that tradition. It will also give you a rough idea about how matplotlib works.

Getting ready

You need to have Python (either v2.7 or v3) and matplotlib installed. You also need to have a text editor (any text editor will do) and a command terminal to type and run commands.

How to do it...

Let's get started with one of the most common and basic graph that any plotting software offers—curves. In a text file saved as plot.py, we have the following code:

import matplotlib.pyplot as plt

X = range(100)
Y = [value ** 2 for value in X]

plt.plot(X, Y)
plt.show()

Tip

Downloading the example code

You can download the sample code files for all Packt books that you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Assuming that you installed Python and matplotlib, you can now use Python to interpret this script. If you are not familiar with Python, this is indeed a Python script we have there! In a command terminal, run the script in the directory where you saved plot.py with the following command:

python plot.py

Doing so will open a window as shown in the following screenshot:

The window shows the curve Y = X ** 2 with X in the [0, 99] range. As you might have noticed, the window has several icons, some of which are as follows:

  • : This icon opens a dialog, allowing you to save the graph as a picture file. You can save it as a bitmap picture or a vector picture.
  • : This icon allows you to translate and scale the graphics. Click on it and then move the mouse over the graph. Clicking on the left button of the mouse will translate the graph according to the mouse movements. Clicking on the right button of the mouse will modify the scale of the graphics.
  • : This icon will restore the graph to its initial state, canceling any translation or scaling you might have applied before.

How it works...

Assuming that you are not very familiar with Python yet, let's analyze the script demonstrated in the previous section.

The first line tells Python that we are using the matplotlib.pyplot module. To save on a bit of typing, we make the name plt equivalent to matplotlib.pyplot. This is a very common practice that you will see in matplotlib code.

The second line creates a list named X, with all the integer values from 0 to 99. The range function is used to generate consecutive numbers. You can run the interactive Python interpreter and type the command range(100) if you use Python 2, or the command list(range(100)) if you use Python 3. This will display the list of all the integer values from 0 to 99. In both versions, sum(range(100)) will compute the sum of the integers from 0 to 99.

The third line creates a list named Y, with all the values from the list X squared. Building a new list by applying a function to each member of another list is a Python idiom, named list comprehension. The list Y will contain the squared values of the list X in the same order. So Y will contain 0, 1, 4, 9, 16, 25, and so on.

The fourth line plots a curve, where the x coordinates of the curve's points are given in the list X, and the y coordinates of the curve's points are given in the list Y. Note that the names of the lists can be anything you like.

The last line shows a result, which you will see on the window while running the script.

There's more...

So what we have learned so far? Unlike plotting packages like gnuplot, matplotlib is not a command interpreter specialized for the purpose of plotting. Unlike Matlab, matplotlib is not an integrated environment for plotting either. matplotlib is a Python module for plotting. Figures are described with Python scripts, relying on a (fairly large) set of functions provided by matplotlib.

Thus, the philosophy behind matplotlib is to take advantage of an existing language, Python. The rationale is that Python is a complete, well-designed, general purpose programming language. Combining matplotlib with other packages does not involve tricks and hacks, just Python code. This is because there are numerous packages for Python for pretty much any task. For instance, to plot data stored in a database, you would use a database package to read the data and feed it to matplotlib. To generate a large batch of statistical graphics, you would use a scientific computing package such as SciPy and Python's I/O modules.

Thus, unlike many plotting packages, matplotlib is very orthogonal—it does plotting and only plotting. If you want to read inputs from a file or do some simple intermediary calculations, you will have to use Python modules and some glue code to make it happen. Fortunately, Python is a very popular language, easy to master and with a large user base. Little by little, we will demonstrate the power of this approach.

Using NumPy


NumPy is not required to use matplotlib. However, many matplotlib tricks, code samples, and examples use NumPy. A short introduction to NumPy usage will show you the reason.

Getting ready

Along with having Python and matplotlib installed, you also have NumPy installed. You have a text editor and a command terminal.

How to do it...

Let's plot another curve, sin(x), with x in the [0, 2 * pi] interval. The only difference with the preceding script is the part where we generate the point coordinates. Type and save the following script as sin-1.py:

import math
import matplotlib.pyplot as plt

T = range(100)
X = [(2 * math.pi * t) / len(T) for t in T]
Y = [math.sin(value) for value in X]

plt.plot(X, Y)
plt.show()

Then, type and save the following script as sin-2.py:

import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(0, 2 * np.pi, 100)
Y = np.sin(X)

plt.plot(X, Y)
plt.show()

Running either sin-1.py or sin-2.py will show the following graph exactly:

How it works...

The first script, sin-1.py, generates the coordinates for a sinusoid using only Python's standard library. The following points describe the steps we performed in the script in the previous section:

  1. We created a list T with numbers from 0 to 99—our curve will be drawn with 100 points.

  2. We computed the x coordinates by simply rescaling the values stored in T so that x goes from 0 to 2 pi (the range() built-in function can only generate integer values).

  3. As in the first example, we generated the y coordinates.

The second script sin-2.py, does exactly the same job as sin-1.py—the results are identical. However, sin-2.py is slightly shorter and easier to read since it uses the NumPy package.

Tip

NumPy is a Python package for scientific computing. matplotlib can work without NumPy, but using NumPy will save you lots of time and effort. The NumPy package provides a powerful multidimensional array object and a host of functions to manipulate it.

The NumPy package

In sin-2.py, the X list is now a one-dimensional NumPy array with 100 evenly spaced values between 0 and 2 pi. This is the purpose of the function numpy.linspace. This is arguably more convenient than computing as we did in sin-1.py. The Y list is also a one-dimensional NumPy array whose values are computed from the coordinates of X. NumPy functions work on whole arrays as they would work on a single value. Again, there is no need to compute those values explicitly one-by-one, as we did in sin-1.py. We have a shorter yet readable code compared to the pure Python version.

There's more...

NumPy can perform operations on whole arrays at once, saving us much work when generating curve coordinates. Moreover, using NumPy will most likely lead to much faster code than the pure Python equivalent. Easier to read and faster code, what's not to like? The following is an example where we plot the binomial x^2 -2x +1 in the [-3,2] interval using 200 points:

import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(-3, 2, 200)
Y = X ** 2 - 2 * X + 1.

plt.plot(X, Y)
plt.show()

Running the preceding script will give us the result shown in the following graph:

Again, we could have done the plotting in pure Python, but it would arguably not be as easy to read. Although matplotlib can be used without NumPy, the two make for a powerful combination.

Plotting multiple curves


One of the reasons we plot curves is to compare those curves. Are they matching? Where do they match? Where do they not match? Are they correlated? A graph can help to form a quick judgment for more thorough investigations.

How to do it...

Let's show both sin(x) and cos(x) in the [0, 2pi] interval as follows:

import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(0, 2 * np.pi, 100)
Ya = np.sin(X)
Yb = np.cos(X)

plt.plot(X, Ya)
plt.plot(X, Yb)
plt.show()

The preceding script will give us the result shown in the following graph:

How it works...

The two curves show up with a different color automatically picked up by matplotlib. We use one function call plt.plot() for one curve; thus, we have to call plt.plot() here twice. However, we still have to call plt.show() only once. The functions calls plt.plot(X, Ya) and plt.plot(X, Yb) can be seen as declarations of intentions. We want to link those two sets of points with a distinct curve for each.

matplotlib will simply keep note of this intention but will not plot anything yet. The plt.show() curve, however, will signal that we want to plot what we have described so far.

There's more...

This deferred rendering mechanism is central to matplotlib. You can declare what you render as and when it suits you. The graph will be rendered only when you call plt.show(). To illustrate this, let's look at the following script, which renders a bell-shaped curve, and the slope of that curve for each of its points:

import numpy as np
import matplotlib.pyplot as plt

def plot_slope(X, Y):
  Xs = X[1:] - X[:-1]
  Ys = Y[1:] - Y[:-1]
  plt.plot(X[1:], Ys / Xs)

X = np.linspace(-3, 3, 100)
Y = np.exp(-X ** 2)

plt.plot(X, Y)
plot_slope(X, Y)

plt.show()

The preceding script will produce the following graph:

One of the function call, plt.plot(), is done inside the plot_slope function, which does not have any influence on the rendering of the graph as plt.plot() simply declares what we want to render, but does not execute the rendering yet. This is very useful when writing scripts for complex graphics with a lot of curves. You can use all the features of a proper programming language—loop, function calls, and so on— to compose a graph.

Plotting curves from file data


As explained earlier, matplotlib only handles plotting. If you want to plot data stored in a file, you will have to use Python code to read the file and extract the data you need.

How to do it...

Let's assume that we have time series stored in a plain text file named my_data.txt as follows:

0  0
1  1
2  4
4 16
5 25
6 36

A minimalistic pure Python approach to read and plot that data would go as follows:

import matplotlib.pyplot as plt

X, Y = [], []
for line in open('my_data.txt', 'r'):
  values = [float(s) for s in line.split()]
  X.append(values[0])
  Y.append(values[1])

plt.plot(X, Y)
plt.show()

This script, together with the data stored in my_data.txt, will produce the following graph:

How it works...

The following are some explanations on how the preceding script works:

  • The line X, Y = [], [] initializes the list of coordinates X and Y as empty lists.

  • The line for line in open('my_data.txt', 'r') defines a loop that will iterate each line of the text file my_data.txt. On each iteration, the current line extracted from the text file is stored as a string in the variable line.

  • The line values = [float(s) for s in line.split()] splits the current line around empty characters to form a string of tokens. Those tokens are then interpreted as floating point values. Those values are stored in the list values.

  • Then, in the two next lines, X.append(values[0]) and Y.append(values[1]), the values stored in values are appended to the lists X and Y.

The following equivalent one-liner to read a text file may bring a smile to those more familiar with Python:

import matplotlib.pyplot as plt

with open('my_data.txt', 'r') as f:
  X, Y = zip(*[[float(s) for s in line.split()] for line in f])

plt.plot(X, Y)
plt.show()

There's more...

In our data loading code, note that there is no serious checking or error handling going on. In any case, one might remember that a good programmer is a lazy programmer. Indeed, since NumPy is so often used with matplotlib, why not use it here? Run the following script to enable NumPy:

import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt('my_data.txt')

plt.plot(data[:,0], data[:,1])
plt.show()

This is as short as the one-liner shown in the preceding section, yet easier to read, and it will handle many error cases that our pure Python code does not handle. The following point describes the preceding script:

  • The numpy.loadtxt() function reads a text file and returns a 2D array. With NumPy, 2D arrays are not a list of lists, they are true, full-blown matrices.

  • The variable data is a NumPy 2D array, which give us the benefit of being able to manipulate rows and columns of a matrix as a 1D array. Indeed, in the line plt.plot(data[:,0], data[:,1]), we give the first column of data as x coordinates and the second column of data as y coordinates. This notation is specific to NumPy.

Along with making the code shorter and simpler, using NumPy brings additional advantages. For large files, using NumPy will be noticeably faster (the NumPy module is mostly written in C), and storing the whole dataset as a NumPy array can save memory as well. Finally, using NumPy allows you to support other common file formats (CVS and Matlab) for numerical data without much effort.

As a way to demonstrate all that we have seen so far, let's consider the following task. A file contains N columns of values, describing N–1 curves. The first column contains the x coordinates, the second column contains the y coordinates of the first curve, the third column contains the y coordinates of the second curve, and so on. We want to display those N–1 curves. We will do so by using the following code:

import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt('my_data.txt')
for column in data.T:
  plt.plot(data[:,0], column)

plt.show()

The file my_data.txt should contain the following content:

0 0 6
1 1 5
2 4 4
4 16 3
5 25 2
6 36 1

Then we get the following graph:

We did the job with little effort by exploiting two tricks. In NumPy notation, data.T is a transposed view of the 2D array data—rows are seen as columns and columns are seen as rows. Also, we can iterate over the rows of a multidimensional array by doing for row in data. Thus, doing for column in data.T will iterate over the columns of an array. With a few lines of code, we have a fairly general plotting generic script.

Plotting points


When displaying a curve, we implicitly assume that one point follows another—our data is the time series. Of course, this does not always have to be the case. One point of the data can be independent from the other. A simple way to represent such kind of data is to simply show the points without linking them.

How to do it...

The following script displays 1024 points whose coordinates are drawn randomly from the [0,1] interval:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.rand(1024, 2)

plt.scatter(data[:,0], data[:,1])
plt.show()

The preceding script will produce the following graph:

How it works...

The function plt.scatter() works exactly like plt.plot(), taking the x and y coordinates of points as input parameters. However, each point is simply shown with one marker. Don't be fooled by this simplicity—plt.scatter() is a rich command. By playing with its many optional parameters, we can achieve many different effects. We will cover this in Chapter 2, Customizing the Color and Styles, and Chapter 3, Working with Annotations.

Plotting bar charts


Bar charts are a common staple of plotting package, and even matplotlib has them.

How to do it...

The dedicated function for bar charts is pyplot.bar(). We will enable this function by executing the following script:

import matplotlib.pyplot as plt

data = [5., 25., 50., 20.]

plt.bar(range(len(data)), data)
plt.show()

The preceding script will produce the following graph:

How it works...

For each value in the list data, one vertical bar is shown. The pyplot.bar() function receives two arguments—the x coordinate for each bar and the height of each bar. Here, we use the coordinates 0, 1, 2, and so on, for each bar, which is the purpose of range(len(data)).

There's more...

Through an optional parameter, pyplot.bar() provides a way to control the bar's thickness. Moreover, we can also obtain horizontal bars using the twin brother of pyplot.bar(), that is, pyplot.barh().

The thickness of a bar

By default, a bar will have a thickness of 0.8 units. Because we put a bar at each unit length, we have a gap of 0.2 between them. You can, of course, fiddle with this thickness parameter. For instance, by setting it to 1:

import matplotlib.pyplot as plt

data = [5., 25., 50., 20.]

plt.bar(range(len(data)), data, width = 1.)
plt.show()

The preceding minimalistic script will produce the following graph:

Now, the bars have no gap between them. The matplotlib bar chart function pyplot.bar() will not handle the positioning and thickness of the bars. The programmer is in charge. This flexibility allows you to create many variations on bar charts.

Horizontal bars

If you are more into horizontal bars, use the barh() function, which is the strict equivalent of bar(), apart from giving horizontal rather than vertical bars:

import matplotlib.pyplot as plt

data = [5., 25., 50., 20.]

plt.barh(range(len(data)), data)
plt.show()

The preceding script will produce the following graph:

Plotting multiple bar charts


When comparing several quantities and when changing one variable, we might want a bar chart where we have bars of one color for one quantity value.

How to do it...

We can plot multiple bar charts by playing with the thickness and the positions of the bars as follows:

import numpy as np
import matplotlib.pyplot as plt

data = [[5., 25., 50., 20.],
  [4., 23., 51., 17.],
  [6., 22., 52., 19.]]

X = np.arange(4)
plt.bar(X + 0.00, data[0], color = 'b', width = 0.25)
plt.bar(X + 0.25, data[1], color = 'g', width = 0.25)
plt.bar(X + 0.50, data[2], color = 'r', width = 0.25)

plt.show()

The preceding script will produce the following graph:

How it works...

The data variable contains three series of four values. The preceding script will show three bar charts of four bars. The bars will have a thickness of 0.25 units. Each bar chart will be shifted 0.25 units from the previous one. Color has been added for clarity. This topic will be detailed in Chapter 2, Customizing the Color and Styles.

There's more...

The code shown in the preceding section is quite tedious as we repeat ourselves by shifting the three bar charts manually. We can do this better by using the following code:

import numpy as np
import matplotlib.pyplot as plt

data = [[5., 25., 50., 20.],
  [4., 23., 51., 17.],
  [6., 22., 52., 19.]]

color_list = ['b', 'g', 'r']
gap = .8 / len(data)
for i, row in enumerate(data):
  X = np.arange(len(row))
  plt.bar(X + i * gap, row,
    width = gap,
    color = color_list[i % len(color_list)])

plt.show()

Here, we iterate over each row of data with the loop for i, row in enumerate(data). The iterator enumerate returns both the current row and its index. Generating the position of each bar for one bar chart is done with a list comprehension. This script will produce the same result as the previous script, but would not require any change if we add rows or columns of data.

Plotting stacked bar charts


Stacked bar charts are of course possible by using a special parameter from the pyplot.bar() function.

How to do it...

The following script stacks two bar charts on each other:

import matplotlib.pyplot as plt

A = [5., 30., 45., 22.]
B = [5., 25., 50., 20.]

X = range(4)

plt.bar(X, A, color = 'b')
plt.bar(X, B, color = 'r', bottom = A)
plt.show()

The preceding script will produce the following graph:

How it works...

The optional bottom parameter of the pyplot.bar() function allows you to specify a starting value for a bar. Instead of running from zero to a value, it will go from the bottom to value. The first call to pyplot.bar() plots the blue bars. The second call to pyplot.bar() plots the red bars, with the bottom of the red bars being at the top of the blue bars.

There's more...

When stacking more than two set of values, the code gets less pretty as follows:

import numpy as np
import matplotlib.pyplot as plt

A = np.array([5., 30., 45., 22.])
B = np.array([5., 25., 50., 20.])
C = np.array([1.,  2.,  1.,  1.])
X = np.arange(4)

plt.bar(X, A, color = 'b')
plt.bar(X, B, color = 'g', bottom = A)
plt.bar(X, C, color = 'r', bottom = A + B)

plt.show()

For the third bar chart, we have to compute the bottom values as A + B, the coefficient-wise sum of A and B. Using NumPy helps to keep the code compact but readable. This code is, however, fairly repetitive and works for only three stacked bar charts. We can do better using the following code:

import numpy as np
import matplotlib.pyplot as plt

data = np.array([[5., 30., 45., 22.],
  [5., 25., 50., 20.],
  [1.,  2.,  1.,  1.]]

color_list = ['b', 'g', 'r']

X = np.arange(data.shape[1])
for i in range(data.shape[0]):
  plt.bar(X, data[i],
    bottom = np.sum(data[:i], axis = 0),
    color = color_list[i % len(color_list)])

plt.show()

Here, we store the data in a NumPy array, one row for one bar chart. We iterate over each row of data. For the ith row, the bottom parameter receives the sum of all the rows before the ith row. Writing the script this way, we can stack as many bar charts as we wish with minimal effort when changing the input data.

Plotting back-to-back bar charts


A simple but useful trick is to display two bar charts back-to-back at the same time. Think of an age pyramid of a population, showing the number of people within different age ranges. On the left side, we show the male population, while on the right we show the female population.

How to do it...

The idea is to have two bar charts, using a simple trick, that is, the length/height of one bar can be negative!

import numpy as np
import matplotlib.pyplot as plt

women_pop = np.array([5., 30., 45., 22.])
men_pop     = np.array( [5., 25., 50., 20.])
X = np.arange(4)

plt.barh(X, women_pop, color = 'r')
plt.barh(X, -men_pop, color = 'b')
plt.show()

The preceding script will produce the following graph:

How it works...

The bar chart for the female population (in red) is plotted as usual. However, the bar chart for the male population (in blue) has its bar extending to the left rather than the right. Indeed, the lengths of the bars for the blue bar chart are negative values. Rather than editing the input values, we use a list comprehension to negate values for the male population bar chart.

Plotting pie charts


To compare the relative importance of quantities, nothing like a good old pie—pie chart, that is.

How to do it...

The dedicated pie-plotting function pyplot.pie() will do the job. We will use this function in the following code:

import matplotlib.pyplot as plt

data = [5, 25, 50, 20]

  plt.pie(data)
plt.show()

The preceding simple script will display the following pie diagram:

How it works...

The pyplot.pie() function simply takes a list of values as the input. Note that the input data is a list; it could be a NumPy array. You do not have to adjust the data so that it adds up to 1 or 100. You just have to give values to matplolib and it will automatically compute the relative areas of the pie chart.

Plotting histograms


Histograms are graphical representations of a probability distribution. In fact, a histogram is just a specific kind of a bar chart. We could easily use matplotlib's bar chart function and do some statistics to generate histograms. However, histograms are so useful that matplotlib provides a function just for them. In this recipe, we are going to see how to use this histogram function.

How to do it...

The following script draws 1000 values from a normal distribution and then generates histograms with 20 bins:

import numpy as np
import matplotlib.pyplot as plt

X = np.random.randn(1000)

plt.hist(X, bins = 20)
plt.show()

The histogram will change a bit each time we run the script as the dataset is randomly generated. The preceding script will display the following graph:

How it works...

The pyplot.hist() function takes a list of values as the input. The range of the values will be divided into equal-sized bins (10 bins by default). The pyplot.hist() function will generate a bar chart, one bar for one bin. The height of one bar is the number of values following in the corresponding bin. The number of bins is determined by the optional parameter bins. By setting the optional parameter normed to True, the bar height is normalized and the sum of all bar heights is equal to 1.

Plotting boxplots


Boxplot allows you to compare distributions of values by conveniently showing the median, quartiles, maximum, and minimum of a set of values.

How to do it...

The following script shows a boxplot for 100 random values drawn from a normal distribution:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(100)

plt.boxplot(data)
plt.show()

A boxplot will appear that represents the samples we drew from the random distribution. Since the code uses a randomly generated dataset, the resulting figure will change slightly every time the script is run.

The preceding script will display the following graph:

How it works...

The data = [random.gauss(0., 1.) for i in range(100)] variable generates 100 values drawn from a normal distribution. For demonstration purposes, such values are typically read from a file or computed from other data. The plot.boxplot() function takes a set of values and computes the mean, median, and other statistical quantities on its own. The following points describe the preceding boxplot:

  • The red bar is the median of the distribution.

  • The blue box includes 50 percent of the data from the lower quartile to the upper quartile. Thus, the box is centered on the median of the data.

  • The lower whisker extends to the lowest value within 1.5 IQR from the lower quartile.

  • The upper whisker extends to the highest value within 1.5 IQR from the upper quartile.

  • Values further from the whiskers are shown with a cross marker.

There's more...

To show more than one boxplot in a single graph, calling pyplot.boxplot() once for each boxplot is not going to work. It will simply draw the boxplots over each other, making a messy, unreadable graph. However, we can draw several boxplots with just one single call to pyplot.boxplot() as follows:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(100, 5)

plt.boxplot(data)
plt.show()

The preceding script displays the following graph:

The pyplot.boxplot() function accepts a list of lists as the input, rendering a boxplot for each sublist.

Plotting triangulations


Triangulations arise when dealing with spatial locations. Apart from showing distances between points and neighborhood relationships, triangulation plots can be a convenient way to represent maps. matplotlib provides a fair amount of support for triangulations.

How to do it...

As in the preceding examples, the following few lines of code are enough:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as tri

data = np.random.rand(100, 2)

triangles = tri.Triangulation(data[:,0], data[:,1])

plt.triplot(triangles)
plt.show()

Every time the script is run, you will see a different triangulation as the cloud of points that is triangulated is generated randomly.

The preceding script displays the following graph:

How it works...

We import the matplotlib.tri module, which provides helper functions to compute triangulations from points. In this example, for demonstration purpose, we generate a random cloud of points using the following code:

data = np.random.rand(100, 2)

We compute a triangulation and store it in the triangles' variable with the help of the following code:

triangles = tri.Triangulation(data[:,0], data[:,1])

The pyplot.triplot() function simply takes triangles as inputs and displays the triangulation result.

Left arrow icon Right arrow icon

What you will learn

  • Discover how to create all the common plots you need
  • Enrich your plots with annotations and sophisticated legends
  • Take control of your plots and master colors, linestyle, and scales
  • Add a dimension to your plots and go 3D
  • Integrate your graphics into your applications
  • Automate your work and generate a large batch of graphics
  • Create interactive plots with matplotlib
  • Combine your plots to create sophisticated visualizations
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 26, 2014
Length: 222 pages
Edition :
Language : English
ISBN-13 : 9781849513265
Category :
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Publication date : Mar 26, 2014
Length: 222 pages
Edition :
Language : English
ISBN-13 : 9781849513265
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 141.97
matplotlib Plotting Cookbook
$48.99
Mastering Object-oriented Python
$48.99
Mastering Matplotlib
$43.99
Total $ 141.97 Stars icon
Banner background image

Table of Contents

8 Chapters
First Steps Chevron down icon Chevron up icon
Customizing the Color and Styles Chevron down icon Chevron up icon
Working with Annotations Chevron down icon Chevron up icon
Working with Figures Chevron down icon Chevron up icon
Working with a File Output Chevron down icon Chevron up icon
Working with Maps Chevron down icon Chevron up icon
Working with 3D Figures Chevron down icon Chevron up icon
User Interface Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7
(10 Ratings)
5 star 30%
4 star 30%
3 star 20%
2 star 20%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Joel Sevilla Mar 24, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I agree with other more experienced reviewers. It is terrific for beginners (like me). Text is clear and the code always run, so the beginner does not have to debug authors code (a task just for advanced users). This book shines between my good list of Python books. Thanks professor Devert.
Amazon Verified review Amazon
Hudan Studiawan Aug 04, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
We are often get confused with text book that provides very complex materials and hard to understand. There are rarely a book that can give us a easy and step-by-step guide to get our job done. Fortunately, Packt Publishing comes with a good news!One of the book provided by Packt is matplotlib Plotting Cookbook. It’s all about plotting your graph with matplotlib. It is an open source library massively used and supported by community worldwide. In this book, you can find any kind of plotting type. Starting from live curve, points, bar chart, pie chart, histograms, and other types of graph. A how-to draw simple graph is given in Chapter 1.This kind of matplotlib library is based on Python programming language. Don’t worry about the difficulty when starting graph for the first time. Python is an intuitive computer programming languange and it is easy to learn.In the next chapter, the author, Alexandre Devert, depicts customization for color and pattern of our chart. We can also control marker style and size, or even creating our own color maps for plotted graph. Like other book published by Packt, all steps are briefly described and very well understood :)In Chapter 3, we will find many instructions to modify annotations for our graph. Formula annotation can be employed using Latex-style notations. The book also describes guides to add label to axis, text, legend, and grid behind the chart. Every part is clearly illustrated by “How to do it” subsection and supported with “How it works … ” part in each section. Chapter 4 will drive us to customization of the figure.We can save our generated graph from matplotlib to file. The supported file format are png, svg, or pdf file. We can follow these guides in Chapter 5. This book is also completed with a story about working with 2D and 3D figures Chapter 6 and 7, respectively. The last chapter, we can integrate our work with Python user interface library. matplotlib have an friendly interface to Tkinter, wxWidgets, GTK, and Pyglet.
Amazon Verified review Amazon
Ty Sid Jun 02, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book has a useful collection of examples for many common use cases that a developer would need in working with Matplotlib. It is written in an easy to follow style that makes it suitable for Python developers that are starting out with Matplotlib. It is not a Python tutorial in any way, but a minimal knowledge of Python is sufficient to understand the code and the examples. I'm relatively new to Python but have significant programming experience in other languages and had no difficulty in picking up the various tools that are presented in the book. Each example comes with complete code listings that just work without any modifications, so one can easily see what is happening with that code. The eBook version comes with code files for all the examples in the book that can be downloaded directly from the Publisher's website (Packt Publishing).I had a bare minimum exposure to Matplotlib but I did not feel that I need to separately following any other tutorial to learn any particular tools. IMO, the given examples provide a better understanding of the most useful tools by seeing them in action and trying them out on your own.The last chapter on User Interfaces covers creating interactive plots using Tkinter, wxWidgets, GTK, etc, which is helpful in bringing the plots to an audience in a way that makes it easier to explain and clarify the data being considered.I would recommend the Matplotlib Plotting Cookbook to people starting out with Matplotlib who want to get up to speed quickly. It's also useful as a quick reference for experienced developers since many common use cases are covered. The index and the table of contents make it simple to find any particular use case.
Amazon Verified review Amazon
Joe Kington Jun 23, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This book is intended to introduce matplotlib to people with relatively little experience with python, and does a good job of getting someone with little experience "up and running" very quickly. The first chapter does a particularly good job of giving a crash course in the basics of scientific programming with python in just a few pages.Overall, this book is easy to follow and does a good job showing concrete examples of most of matplotlib's functionality. Many of the "There's more" and "How it works" sections do a great job of explaining things in a bit more detail (though I do wish they were longer). There's a nice linear flow to the way the chapters are laid out, and for something called a "cookbook", it's more of an "introduction through example" (which is a good thing!).However, I'm not sure this book adds much over the official documentation. There are a lot of examples, but not enough explanation of the underlying concepts (then again, it is a "cookbook" and not "the exhaustive guide to matplotlib", so perhaps I'm being a bit unfair). While matplotlib is a bit disorganzied in some regards, there are several things that are common throughout the library that are useful to know. For example, it would be nice if this book more prominently mentioned that the plotting commands all return "artists" whose properties can be adjusted through the "artist.set(...)" function.A couple of minor pet peeves:1) The author sticks almost entirely to the pyplot interface throughout the book, but then suddenly changes over to the standard object-oriented interface part-way through (beginning with the 3D plotting chapter). It would be good to explain the change. Better yet, explain what the difference is and the relative advantages of the two.2) The chapters are rather misleadingly named. For example, Chapter 6 is titled "Working with Maps", but doesn't cover matplotlib's mapping capabilities (e.g. basemap or cartopy - both dealing with geographic maps) at all. Instead, it deals with image data and vector plots (e.g. "imshow", "quiver", etc). Similarly Chapter 3 - "Working with Annotations" is a great chapter, but the title is a bit misleading: it deals with adding low-level artists like patches and lines as well as adding latex-formatted equations, title, labels, etc. (Again, I'm being a bit picky, but being able to accurately scan the table of contents is very important in technical books.)
Amazon Verified review Amazon
Mgx May 08, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
First of all, the book is very easy to read. Most of the book is filled with recipes code examples and their illustrated outputs. So, it would be fairly simple to complete this book in a day or two.Secondly, I agree with the other review that this book would be quite useful to beginners who are just starting out with Python + Matplotlib. In addition, there are a few recipes inside that could save time for intermediate and expert users. For example, I found some of the recipes for 3D plotting useful, but your mileage could vary depending on your data. The last few recipes show how to interface Matplotlib with Python GUI interfaces and this could also be helpful.However, I feel that the price of the book is high ($40+) considering the material it offers. After all, Matplotlib is technically free to use and has free, well-presented documentation online. This book does not substitute the online documentation at all - rather, it presents a subset of the most useful instructions to get things shaking fast.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela