Social Icons

Monday, February 24, 2020

Python Matplotlib module

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 (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 


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])
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)

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]

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 

5. Drawing of bar graphs and histograms

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


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

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]

Younes Derfoufi

No comments:

Post a Comment

Learn Python For Beginners

Category Of Mobile Courses

Actualités (644) Adsense (1) Affiliation (1) Algebraic Topology (2) Algorithmic (1) all-news (30) Android (5) Android App (8) Android app without code (4) Android Apps (256) Android Development (4) Android download (2) Android OS (3) AngularJS (1) Automata theory and formal language (5) Bootstrap CSS (1) C programming (5) Category and Functor (8) CMS (3) Computer Glossary (18) Create Mobile App With Ionic Framework (2) CSS (2) CSS-Cascading-Style-Sheets (4) Developpement Java (13) Differential Geometry (1) Django-Python-Framework (15) dropshiping (26) Earn Money by Internet (4) Emplois (23) Framework php (2) Fraud (2) Github (2) HTML (9) IT News (3) Java For Beginners (10) Javascript (12) Kotlin Programming Language (8) Kotlin For Mobile Android (1) Linux Download (2) Marketing (5) Mobile (3) Mobile Courses (4) Mobile Marketing (4) MoneyGram (1) News (721) Node.js (5) Open Source (1) Photoshop (1) Protect Computer (1) Python (37) Python BeautifulSoup (1) Python For Data Science (2) Python PyQt (13) Python Reference (1) Python Source Code (10) Python-Books (6) Python-DVD-Training (1) Python-Exercises (305) Python-Framework (1) Python-IDE (1) Python-Kivy-Framework (2) Python-Modules (1) Python-pdf (2) Python-pyQt (1) python-temp (3) Référencement (2) Script PHP (2) Security (6) SEO (1) Snipping Tool: Faq (1) Social Networks (1) Source Code (4) Statistics With SPSS (2) Surveillance Software (1) Travail à domicile (6) Tutoriels php en vidéos (2) Tutoriels-MySql (6) tutoriels-php (19) Utilitaires (1) VPS (1) Web Hosting (1) Webcam (1) Webmarketing (11) Western Union (1) Windows 10 (1) Windows 7 (4) Windows 7 Faq (2) Windows 8 (1) Windows Accessories (1) Windows Download (8) Windows Drivers (1) Windows Fonts (1) Windows Power Shell (2) Windows Registry (2) Windows Security (18) Windows Software (2) Windows Spyware (2) Windows utilities (3) Windows Virus (2) Windows Vista (3) Windows Wireless (1) Windows xp (1) Wordpress (1)

Sample text

Sample Text

Blogger Templates