Getting Started with Matplotlib

Getting Started with Matplotlib

,QWKHSUHYLRXVFKDSWHUZHKDYHJLYHQDEULHILQWURGXFWLRQWR0DWSORWOLE:HQRZ want to start using it for real--after all, that's what you are reading this book for. In this chapter, we will:

Explore the basic plotting capabilities of Matplotlib for single or multiple lines

Add information to the plots such as legends, axis labels, and titles 6DYHDSORWWRD?OH Describe the interaction with IPython &XVWRPL]H0DWSORWOLEERWKWKURXJKFRQ?JXUDWLRQ?OHVDQG3\WKRQFRGH Let's start looking at some graphs.

First plots with Matplotlib

One of the strong points of Matplotlib is how quickly we can start producing plots RXWRIWKHER[+HUHLVRXU?UVWH[DPSOH

$ ipython In [1]: import matplotlib.pyplot as plt In [2]: plt.plot([1, 3, 2, 4]) Out[2]: [] In [3]: plt.show()

This material is copyright and is licensed for the sole use by Jillian Fraser on 20th November 2009

Getting Started with Matplotlib

This code snippet gives the output shown in the following screenshot:

As you can see, we are using IPython. This will be the case throughout the book, so we'll skip the command execution line (and the heading) as you can easily recognize IPython output. Let's look at each line of the previous code in detail:

In [1]: import matplotlib.pyplot as plt

This is the preferred format to import the main Matplotlib submodule for plotting, pyplot. It's the best practice and in order to avoid pollution of the global namespace, it's strongly encouraged to never import like:

from import *

7KHVDPHLPSRUWVW\OHLVXVHGLQWKHRI?FLDOGRFXPHQWDWLRQVRZHZDQWWREH consistent with that.

In [2]: plt.plot([1, 3, 2, 4])

[ 22 ]

This material is copyright and is licensed for the sole use by Jillian Fraser on 20th November 2009

Chapter 2

7KLVFRGHOLQHLVWKHDFWXDOSORWWLQJFRPPDQG:HKDYHVSHFL?HGRQO\DOLVWRIYDOXHV that represent the vertical coordinates of the points to be plotted. Matplotlib will XVHDQLPSOLFLWKRUL]RQWDOYDOXHVOLVWIURPWKH?UVWYDOXH WR1ZKHUH1LVWKH number of items in the list). If you remember from high school, the vertical values represent the Y-axis while the horizontal values are the X-axis, and what we do is called "to plot Y against X".

In [3]: plt.show()

This command actually opens the window containing the plot image. Of course, we can also explicitly specify both the lists:

In [1]: import matplotlib.pyplot as plt In [2]: x = range(6) In [3]: plt.plot(x, [xi**2 for xi in x]) Out[3]: [] In [4]: plt.show()

that results in the following screenshot:

[ 23 ]

This material is copyright and is licensed for the sole use by Jillian Fraser on 20th November 2009

Getting Started with Matplotlib

As we can see, the line shown in the previous screenshot has several edges, while we might want a smoother parabola. So, we can start introducing the interaction with NumPy with one of its most used functions, arange(), and highlighting the difference with range():

range(i, j, k) is a Python built-in function that generates a sequence of integers from i to j with an increment of k (both, the initial value and the step are optional).

arange(x, y, z) is a part of NumPy, and it generates a sequence of elements (with data type determined by parameter types) from x to y with a spacing z (with the same optional parameters as that of the previous function).

So we can use arange()WRJHQHUDWHD?QHUUDQJH In [1]: import matplotlib.pyplot as plt In [2]: import numpy as np In [3]: x = np.arange(0.0, 6.0, 0.01) In [4]: plt.plot(x, [x**2 for x in x]) Out[4]: [] In [5]: plt.show()

[ 24 ]

This material is copyright and is licensed for the sole use by Jillian Fraser on 20th November 2009

Chapter 2

Multiline plots

It's fun to plot a line, but it's even more fun when we can plot more than one line RQWKHVDPH?JXUH7KLVLVUHDOO\HDV\ZLWK0DWSORWOLEDVZHFDQVLPSO\SORWDOO the lines that we want before calling show(). Have a look at the following code and screenshot:

In [1]: import matplotlib.pyplot as plt In [2]: x = range(1, 5) In [3]: plt.plot(x, [xi*1.5 for xi in x]) Out[3]: [] In [4]: plt.plot(x, [xi*3.0 for xi in x]) Out[4]: [] In [5]: plt.plot(x, [xi/3.0 for xi in x]) Out[5]: [] In [6]: plt.show()

Note how Matplotlib automatically chooses different colors for each line--green for WKH?UVWOLQHEOXHIRUWKHVHFRQGOLQHDQGUHGIRUWKHWKLUGRQHIURPWRSWRERWWRP &DQ\RXWHOOZK\D?RDWYDOXHZDVXVHGLQOLQH>@"7U\LW\RXUVHOIZLWKDQLQWHJHU one and you'll see. The answer is that if divided by 3 (that is, by using an integer FRHI?FLHQW WKHUHVXOWZLOODOVREHDQLQWHJHU6R\RX

OOVHHDOLQHSORWOLNHVWDLUV

[ 25 ]

This material is copyright and is licensed for the sole use by Jillian Fraser on 20th November 2009

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download