Aller au contenu

Prise en main de Matplotlib - Le guide ultime

Matplotlib est l'emblématique package Python qui permet des créer des visualisations avec Python. Matplotlib est une libraire Python de visualisation.

Installer Matplotlib

Si vous travaillez avec Anaconda, Matplotlib est déjà installé sinon Matplotlib est disponible sur le répertoire PyPi

!pip install matplotlib

Importer Matplotlib

Matplotlib n'est pas une librairie native en Python, il faut l'installer au préalable. Si vous ne l'avez pas encore installée avez pas Pour importer Matplotlib if suffit de faire ceci import matplotlib mais il est plus courant de faire ceci import matplotlib.pyplot as plt. Nous nous concentrerons sur le package .pyplot

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

Généralement, on importe ensemble ces trois librairies

names = pd.Series(["Esso", "Lendjina", "Yemi", "Adboul", "Tracy", "Adjo", "Yacuba", "Degnon"])
countries = pd.Series(["Togo", "Haïti", "Nigeria", "Burkina", "Ivory Cost", "Togo", "Niger", "Benin"])
sexes = pd.Series(['M', 'F', 'M', 'M', 'F', 'F', 'F', 'F'])
ages = pd.Series([22, 18, 20,19, 31, 23, 26, 17])
heights = pd.Series([1.73, 1.43, 1.52, 1.82, 1.45, 1.61, 1.90, 1.52])

1. Diagramme en bâtons matplotlib.pyplot.bar - matplotlib.pyplot.barh

plt.bar(labels, values)
plt.show()

plt.barh(labels, values)
plt.show()
No description has been provided for this image
No description has been provided for this image

2. Diagramme en secteurs matplotlib.pyplot.pie

labels = sexes.value_counts().index
values = sexes.value_counts().values
plt.pie(x=values, labels=labels, autopct='%.2f')
plt.show()
No description has been provided for this image

matplotlib.pyplot.hist

Histograme

# Distribution gaussienne de moyenne 5 et d'écart-type 4
x = np.random.normal(5, 4, 100)
plt.hist(x)
plt.title("Distribution gaussienne de moyenne 5 et d'écart-type 4")
plt.show()

# Distribution exponentielle de paramètre lambda=3
x = np.random.exponential(3, 100)
plt.hist(x)
plt.title("Distribution exponentielle de paramètre lambda=3")
plt.show()

# Distribution uniforme de paramètres a=4 et b=5
x = np.random.uniform(4, 5, 100)
plt.hist(x)
plt.title("Distribution uniforme de paramètres a=4 et b=5")
plt.show()
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image

matplotlib.pyplot.scatter

Nuage de points

x = np.random.random(100)
y = np.random.random(100)

plt.scatter(x, y)
plt.show()
No description has been provided for this image

matplotlib.pyplot.plot

Courbes

x = np.linspace(-10, 10, 100)
y = np.sin(x)

plt.plot(x, y, label='sin(x)', color='black')
plt.legend(loc='best')
plt.show()
No description has been provided for this image
from mpl_toolkits.mplot3d import Axes3D

X = np.random.normal(2, 3, 100)
Y = np.random.uniform(1, 5, 100)
Z = np.random.random(100)

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(X, Y, Z)

plt.show()
No description has been provided for this image
X = np.linspace(0.1, 10, 100)
Y = np.linspace(0.1, 10, 100)
Z = np.log(X + Y)

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(X, Y, Z)

plt.show()
No description has been provided for this image
Z
array([       -inf, -1.59938758, -0.9062404 , -0.50077529, -0.21309322,
        0.01005034,  0.19237189,  0.34652257,  0.48005397,  0.597837  ,
        0.70319752,  0.7985077 ,  0.88551907,  0.96556178,  1.03966975,
        1.10866262,  1.17320115,  1.23382577,  1.29098418,  1.3450514 ,
        1.3963447 ,  1.44513486,  1.49165488,  1.53610664,  1.57866625,
        1.61948825,  1.65870896,  1.69644929,  1.73281693,  1.76790825,
        1.80180981,  1.83459963,  1.86634833,  1.89711998,  1.92697295,
        1.95596048,  1.98413136,  2.01153034,  2.03819858,  2.06417407,
        2.08949188,  2.11418449,  2.13828204,  2.16181254,  2.18480206,
        2.20727491,  2.22925382,  2.25076003,  2.27181343,  2.29243272,
        2.31263543,  2.33243806,  2.35185614,  2.37090434,  2.38959647,
        2.40794561,  2.42596411,  2.44366369,  2.46105543,  2.47814987,
        2.49495699,  2.51148629,  2.52774681,  2.54374715,  2.55949551,
        2.57499969,  2.59026717,  2.60530504,  2.62012013,  2.63471893,
        2.64910767,  2.6632923 ,  2.67727854,  2.69107186,  2.70467752,
        2.71810054,  2.73134576,  2.74441785,  2.75732125,  2.77006028,
        2.78263906,  2.79506158,  2.80733167,  2.81945303,  2.83142922,
        2.84326368,  2.85495972,  2.86652054,  2.87794924,  2.88924879,
        2.90042209,  2.91147193,  2.922401  ,  2.93321192,  2.94390721,
        2.95448932,  2.96496061,  2.9753234 ,  2.9855799 ,  2.99573227])

Boîte à moustache

Histogramme

Carte de chaleur

Références


Pour avoir plus de détails, merci de consulter la documentation officielle Numpy Docs