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 03 04 t = np.arange(0., 5., 0.2) 05 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') 09 10 plt.legend() 11 plt.title("Different rates of growth") 12 plt.savefig('basic-styling.png') 13 plt.show()

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.

Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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
News
-
An All-Snap Version of Ubuntu is In The Works
Along with the standard deb version of the open-source operating system, Canonical will release an-all snap version.
-
Mageia 9 Beta 2 Ready for Testing
The latest beta of the popular Mageia distribution now includes the latest kernel and plenty of updated applications.
-
KDE Plasma 6 Looks to Bring Basic HDR Support
The KWin piece of KDE Plasma now has HDR support and color management geared for the 6.0 release.
-
Bodhi Linux 7.0 Beta Ready for Testing
The latest iteration of the Bohdi Linux distribution is now available for those who want to experience what's in store and for testing purposes.
-
Changes Coming to Ubuntu PPA Usage
The way you manage Personal Package Archives will be changing with the release of Ubuntu 23.10.
-
AlmaLinux 9.2 Now Available for Download
AlmaLinux has been released and provides a free alternative to upstream Red Hat Enterprise Linux.
-
An Immutable Version of Fedora Is Under Consideration
For anyone who's a fan of using immutable versions of Linux, the Fedora team is currently considering adding a new spin called Fedora Onyx.
-
New Release of Br OS Includes ChatGPT Integration
Br OS 23.04 is now available and is geared specifically toward web content creation.
-
Command-Line Only Peropesis 2.1 Available Now
The latest iteration of Peropesis has been released with plenty of updates and introduces new software development tools.
-
TUXEDO Computers Announces InfinityBook Pro 14
With the new generation of their popular InfinityBook Pro 14, TUXEDO upgrades its ultra-mobile, powerful business laptop with some impressive specs.