1. About matplotlib module

The matplotlib module is a large and very complete module. Here we are going to look at some examples of use such as for example the plotting of graphical representations of functions or statistical series. More information on official documentation can be found here.

2. Importing the matplotlib & pyplot module. 

Like all modules, it must be loaded and more precisely it is a sub-module that will interest us: pyplot. For that, we will not load all the functions as usual but import it under a shorter name to use. We will therefore use import matplotlib.pyplot as plt. This means that to use a function of this module like show() for example, we will have to write plt.show() (since we imported the module under the name plt). In addition, the matplotlib module is very much linked to another module which is used to make numerical computation which is called numpy and which is often imported under the name np. In summary, if you want to graph functions or others, you will need to put in the header:

import matplotlib.pyplot as plt
import numpy as np

3. Basic functions 

3.1 plt.show(): 

To display the result. All the following functions are used to prepare the graph but if we do not ask to display it, nothing will happen (exactly like the print function: no calculation is displayed if we do not ask to display it with print) .plt.plot(liste_x, liste_y): Where liste_x is a list of numbers [x_1, x_2, ..., x_n] and liste_y is a list of numbers [y_1, y_2, ..., y_n] with the same number of elements. Then plt.plot (liste_x, liste_y) will place the points of coordinates (x_1, y_1), (x_2, y_2), ..., (x_n, y_n) and connect them step by step by a segment. Here is an example where we connect the points (1, 3), (4, 2) and (5, 7):

Example by using plot() method

import matplotlib.pyplot as plt
import numpy as np
plt.plot ([1,4,5], [3,2,7])
plt.show()

You can modify the point lists in the program above to see the result.
The idea to draw a function will therefore be to place a lot of points on the curve that we want to represent close enough so that we don't see that they are connected by a straight line.

3.2 np.linspace(start, end, N)

This is where the numpy module comes in. To correctly plot a function, we will need a lot of points that it is out of the question to enter by hand as in the previous example. The np.linspace (start, end, number) function allows you to create a list of N numbers which start at the start value and stop at the end value and are evenly distributed.
For example, let's draw the function defined by y = sin(x) between -5 and 5 using 100 points:


import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5, 5, 100)
y = np.sin(x)
plt.plot(x,y)
plt.show()

3.3 - plt.axis(x_min, x_max, y_min, y_max): 

This function allows you to modify the axes of the coordinate system that will be displayed. If it is not used, the choice of axes will be made automatically but sometimes this choice is not relevant and it will therefore have to be modified with this function. The first two values given are the minimum and maximum values for the x-axis and the next two are those for the y-axis.

3.4 - plt.grid(): 

Displays an additional grid on our coordinate system.

4. Plotting more complex functions

Suppose we want to draw functions involving something other than the operations +, -, * such as logarithm, exponential, cosinus, sinus... In this case we cannot do exactly as in the previous example.

4.1 First method

A first way of doing this is to create manualy the list of y corresponding to the x, that is to say create a list composed of the f (x) for x in the list of abscissas.
For example if we want to plot the function y = 2*sin(x) + x between 0 and 7, we could do so:

import matplotlib.pyplot as plt
import numpy as np
from math import *
abscissa = np.linspace(0,7,100)
ordinate = [2*sin(x) + x for x in abscissa]
plt.plot(abscissa,ordinate)
plt.show()

4.2 Second method

A second method consists in using the modified classical functions contained in numpy by using the syntax np.function():

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,7,100)
y = 2*np.sin(x) + x
plt.plot(x,y)
plt.show()

5. Drawing of bar graphs and histograms

Bar graphs and histograms can be traced  easily from data such as statistical series.

Example

import matplotlib.pyplot as plt
import numpy as np
x = [1,2,3,4,5]
height = [7,11,9,3,6]
width = 0.5
plt.bar(x, height, width )
plt.show()

6. Displays point cloud

To draw points, just use the function plt.scatter (abscissas, ordinates) where abscissas is the list of abscissas of the points we want to plot and ordinates the list of ordinates.
For example if you want to place points (1,3), (2,2.7), (3,3.5), (4,3), (5,3) and (6,4):

import matplotlib.pyplot as plt
import numpy as np
x = [ 1, 2, 3, 4, 5, 6]
y = [ 3, 2.7, 3.5, 3, 3, 4]
plt.scatter(x,y)
plt.show()

Younes Derfoufi
my-courses.net

Leave a Reply