A plotting library

Data Structures

As far as data structures are concerned, no matter how you define and fill a sequence of values, Matplotlib will internally convert it to the special arrays provided by NumPy, because those are the formats that its plotting functions can handle. In most cases, you should directly use the same arrays anyway, as shown in the examples that follow, because they are faster to process than normal Python arrays and consume less memory.

Matplotlib Gallery

With the general concepts presented above in mind, I will use some simple, but very different, examples to create plots with Matplotlib. You can easily adapt the following examples to a variety of use cases.

A Basic Plot with Line Styling

The Matplotlib code in Listing 3 generates the plot shown in Figure 1. After loading pyplot and NumPy in the first two lines, Listing 3 defines a variable t that increases in steps of 0.2 units in the interval from   to 5 (line 4). Lines 6 to 8 show how to plot three different curves, each with its unique style and label. The format of the strings used to style the lines is hard to memorize because it has lots of options, but it's not cryptic once you know that the first letter is the color and the rest are a more or less symbolic alias for the line style: 'r--' creates a dashed red line, 'bs' creates another line marked with blue squares, and 'g^' creates a third line made of green triangles. The default format string is 'b-', which is a solid blue line. Lines 10 to 12 add the legend and title and save the plot to a file.

Listing 3

Basic Plot

01 import Matplotlib.pyplot as plt
02 import NumPy as np
04 t = np.arange(0., 5., 0.2)
06 plt.plot(t, t,'r--',label='Linear')
07 plt.plot(t, t**2, 'bs', label='Quadratic')
08 plt.plot(t, t**3, 'g^', label='Cubic')
10 plt.legend()
11 plt.title("Different rates of growth")
12 plt.savefig('basic-styling.png')
13 plt.show()
Figure 1: A really basic plot still looks nice thanks to Matplotlib styling options.

Line 13 of Listing 3 opens the chart in the graphical interface shown in Figure 2, which, at least on Ubuntu, comes with the standard Matplotlib package. In this interface, aside from modifying the styles, you can also zoom in, drag the figure around to look at a specific zone, change the size of any subplot, and save the results of your edits.

Figure 2: The basic Matplotlib interface for Linux lets you try out different formatting options.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Data Visualization in Python

    Python's powerful Matplotlib, Bokeh, PyQtGraph, and Pandas libraries lend programmers a helping hand when visualizing complex data and their relationships.

  • Unsupervised Learning

    The most tedious part of supervised machine learning is providing sufficient supervision. However, if the samples come from a restricted sample space, unsupervised learning might be fine for the task.

  • Tutorial – Prettymaps

    Prettymaps combines multiple Python libraries to make it easy to draw maps straight from the OpenStreetMap database.

  • Machine Learning

    We explore some machine learning techniques with a simple missing person app.

  • Gnuplot

    Use Gnuplot with command-line utilities.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters