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()
![](/var/linux_magazin/storage/images/issues/2022/257/tutorial-matplotlib/figure-1/801526-1-eng-US/Figure-1_large.png)
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.
![](/var/linux_magazin/storage/images/issues/2022/257/tutorial-matplotlib/figure-2/801529-1-eng-US/Figure-2_large.png)
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.