[EN] Installing NumPy and Matplotlib on Raspberry Pi

This article guides on installing the NumPy and Matplotlib libraries on the Raspberry Pi board for computing (Read an example of using numpy for small devices in the ulab article) and shows results. The article discusses installation, upgrades and basic usage to guide further implementation. As for the use of Raspberry Pi with Python programming language, you can find more information from various knowledge sources or support our team by purchasing a book or finding a Raspberry Pi 4 4GB board that can be purchased from various retailers or from here, Though, we use a Raspberry Pi 3 B+.

Preparation

The use of the NumPy and Matplotlib libraries requires a Python programming language installed on the Raspberry Pi board. In this article, Python 3 is the primary coding tool. The first step requires installing python3 and pip with the following commands.

sudo apt install python3-dev python3-pip

when installed, run the following command to have the python compiler report the version number as an example in Figure 1.

python3 –version

(Figure. 1 installing python3 and pip3)

NumPy

NumPy was developed by Travis Oliphant in 1995 as a library or collection or extension of the Python programming language which was open source project. Focus on working with 1D or multidimensional matrix or arrays. It also provides mathematical commands to work with the matrix or array. Additionally, NumPy runs through Cython, a Python-based C library method. This is the highlight of NumPy because executing in C is faster than writing in Python due to NumPy focuses on processing the entire dataset therefore, it must be looped to calculate with every member in the array. This makes Python work slower. However, running between C and Python can be slower with frequent callbacks. And having programming skills will allow you to choose the method that makes the best possible processing.

NumPy stores data in the form of arrays that were called ndarray which is different from Python’s List structure because ndarray requires the number and type of data members to be specified making it impossible to increase or decrease members and all members must be of the same type.

Here are some rough features that NumPy can do:

  1. Support array of integer/decimal/complex number data.
  2. Process the entire array in a single command.
  3. Supports basic calculations such as addition, subtraction, multiplication and division.
  4. There are commands to create custom arrays and set to a range of both integers and decimals.
  5. Support for math commands from math class library, but focus on working with whole arrays such as sin, cos, tan etc.
  6. Linear Algebra
  7. Comparison
  8. Dealing with numerical data such as flip roll, etc.
  9. Estimation
    etc.

Installing NumPy

กTo install NumPy using apt, run the following commands:

sudo apt install python3-numpy

To install NumPy using pip3, run the following command.

sudo pip3 install numpy

Upgrading NumPy

Upgrading NumPy with apt can be run as follows:

sudo apt update
sudo apt upgrade

Upgrading NumPy with pip3 can be executed to upgrade with the following command.

sudo pip3 install –upgrade numpy

numpy test

The example code20-1 creaetes a vector or a 1D array of the sine function from an angle of 0 degrees to 179 degrees with a noise value that does not exceed -0.01 to 0.01. The results of both vectors are shown in Figure 2.

#code20-1
import numpy as np
import math
ang = np.arange(180)
xo = np.sin(np.radians(ang))
noise = ((np.random.rand(180)*2)-1.0)/100.0
xn = xo+noise
print("xo = {}".format(xo))
print("xo+noise = {}".format(xn))
(Figure. 2 result of code20-1)

Matplotlib

Matplotlib is a Python language library that is a math extension from NumPy to create charts or graphs in a GUI (Graphics User Interface) based on Tkinter, wxPython, Qt or GTK+ developed by John D. Hunter

Installing Matplotlib

To install Matplotlib with apt do the following:

sudo apt install python3-matplotlib python3-tk

To install Matplotlib with pip3 do the following:

sudo pip3 install matplotlib pytk

Note
Matplotlib requires pytk and a Tkinter suite.

Upgrading Matplotlib

Upgrading Matplotlib via apt runs as follows:

sudo apt update
sudo apt upgrade

Upgrading Matplotlib via pip3 can be performed with the following command:

sudo pip3 install –upgrade matplotlib

Matplotlib test

The example code20-2 takes data from code20-1 and plots it as a graph. The result is shown in Figure 2.

#code20-2
import numpy as np
import math
import matplotlib.pyplot as plt
ang = np.arange(180)
xo = np.sin(np.radians(ang))
noise = ((np.random.rand(180)*2)-1.0)/100.0
xn = xo+noise
plt.subplot(121)
plt.ylabel('sine')
plt.xlabel('degrees')
plt.plot(xo)
plt.subplot(122)
plt.ylabel('sine')
plt.xlabel('degrees')
plt.plot(xn)
plt.suptitle('code20-2')
plt.show()
(Figure. 3 result of code20-2)

Conclusion

From this article, readers can install NumPy and Matplotlib and write a sample program to test the program’s functionality. You will find that Python tools are readily available and easy to use. Plus, the code can run across the operating systems. This saves time in developing new works whenever migrating and further coding.

Finally, we hope this article will be more or less useful to the readers. and have fun with programming

Reference

  1. numpy
  2. WiKi numpy
  3. Introduction to numpy
  4. matplotlib

(C) 2020, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2021-08-25