A plotting library
Reading Data from a CSV File
You can use Matplotlib plots to visualize data created in another program and saved as a file, often as plain text in comma-separated values (CSV) format, such as csv-data.csv
in Listing 4.
Listing 4
csv-data.csv
John,32 Mary,48 Ashok,25 Mario,41 Heather,38 Ashley,29
Figure 3 shows the data from the CSV file in Listing 4 in a histogram format. To create the histogram with Matplotlib, you can use the code in Listing 5 – after loading Matplotlib and another module (csv) to hande CSV data. Listing 5 creates two arrays, x
and y
(lines 4 and 5), and opens the data file (line 7). Lines 8-12 load the file's contents into a table called plots
(using the comma as a column separator), copy the names from the first column into the x
array and the ages (reformatted as integer numbers) into the y
array. The rest of Listing 5 is self-explanatory, because each line corresponds to a specific, easy-to-spot visual feature in Figure 3.
Listing 5
Plotting Data from a CSV File
01 import Matplotlib.pyplot as plt 02 import csv 03 04 x = [] 05 y = [] 06 07 with open('csv-data.csv','r') as csvfile: 08 plots = csv.reader(csvfile, delimiter = ',') 09 10 for row in plots: 11 x.append(row[0]) 12 y.append(int(row[1])) 13 14 plt.bar(x, y, color = 'g', width = 0.72, label = "Age") 15 plt.xlabel('Names') 16 plt.ylabel('Ages') 17 plt.title('Customers ages') 18 plt.legend() 19 plt.savefig('03-csv.png') 20 plt.show()
A Random Data, Scattered Plot
While an in-depth overview of NumPy's capabilities is beyond the scope of this tutorial, Figure 4 gives an idea of the possibilities of using Matplotlib and NumPy together. The scatter plot in Figure 4 is generated from the code in Listing 6, which is taken (with minimal changes) from the official Matplotlib documentation. For my purposes, I will divide the code into two sections and discuss them in reverse order. The last section (lines 11-15) draws and saves a series of distinct points, scattered in random positions across the Axes. The coordinates, size, and color of each point are loaded in line 12 by the powerful pyplot function scatter
(see [6] for an explanation of scatter
syntax). The interesting part of Listing 6 is how all the arrays of values passed to scatter
are generated. It is easy to guess, by looking at lines 4-9, that all those arrays are generated randomly, on the spot, with simple calls to the NumPy functions that generate random numbers. For the settings of those functions, please see the documentation for these functions on the NumPy website [4].
Listing 6
Scattered, Random Data
01 import Matplotlib.pyplot as plt 02 import NumPy as np 03 04 np.random.seed(20010911) 05 data = {'a': np.arange(50), 06 'c': np.random.randint(0, 50, 50), 07 'd': np.random.randn(50)} 08 data['b'] = data['a'] + 10000 * np.random.randn(50) 09 data['d'] = np.abs(data['d']) * 100 10 11 fig, ax = plt.subplots(figsize=(5, 2.7)) 12 ax.scatter('a', 'b', c='c', s='d', data=data) 13 ax.set_xlabel('Speed') 14 ax.set_ylabel('Distance'); 15 plt.savefig('random-scatter.png')
Subplots and Extra Axes
You can combine different plots into one Matplotlib Figure, add Axis elements, and link them to each other using the techniques in Listing 7.
Listing 7
Connecting Subplots
01 import Matplotlib.pyplot as pls 02 import NumPy as np 03 04 t = np.arange(0.0, 40.0, 0.8) 05 s = t**2 #parabola 06 07 fig, (ax1, ax3) = plt.subplots(1, 2, figsize=(12, 5)) 08 l1, = ax1.plot(t, s) 09 ax2 = ax1.twinx() 10 l2, = ax2.plot(t, np.cos(-1*t/2), 'C1') 11 ax2.legend([l1, l2], ['Parabola (left)', 'Sinusoid (right)']) 12 13 ax3.plot(t, 20 - t*np.sin(20 - t)) 14 ax3.set_xlabel('Angle [∞]') 15 16 ax4 = ax3.secondary_xaxis('top', functions=(np.rad2deg, np.deg2rad)) 17 ax4.set_xlabel('Angle [rad]') 18 19 plt.savefig('extra-axes.png')
Using more or less the same syntax shown in the previous examples, Listing 7 creates three distinct functions: a parabola (lines 5 and 8), a normal sinusoid (line 11), and a growing sinusoid (line 13). What is new is that these plots are grouped into two subplots (shown in Figure 5), whose Axis objects are indeed linked to each other. A full explanation of the code is beyond the scope of this article, but here are the main points you need to understand to create similar charts.
Line 7 splits the main Figure into two subplots, each with its own Axes, called ax1
and ax3
. Then, the first subplot gets the ax2 of line 9, which is a "twin" of the ax1 of line 8, for a very precise purpose: to show the very different scales of both the parabola and the sinusoid separately, but as clearly as possible, on opposite sides of the subplot. Meanwhile, the growing sinusoid is plotted in the right subplot, with its own labels and titles, but also using another feature of Matplotlib (line 16): the secondary_xaxis
method creates an extra horizontal Axis (ax4
) at the top of that subplot that shows the same variable as the bottom Axis (ax3
) but with a different unit (radiants instead of degrees).
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.
News
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.