Open In Colab

15: Matplotlib Top Graphs#

import numpy as np
import matplotlib.pyplot as plt

1.Classification Graph with Scatter()#

from sklearn.datasets import load_iris
iris = load_iris()
x = iris.data
y = iris.target

print(f'x contains {x.shape[0]} examples and {x.shape[1]} variabels')
print(f'There are {np.unique(y).size} classes')
x contains 150 examples and 4 variabels
There are 3 classes
n = x.shape[1]
plt.figure(figsize=(12, 8))
for i in range(n):
    plt.subplot(n//2, n//2, i+1)
    plt.scatter(x[:, 0], x[:, i], c=y)
    plt.xlabel('0')
    plt.ylabel(i)
    plt.colorbar(ticks=list(np.unique(y)))
plt.show()
../../../_images/df46d20b38144b265cd2545c707775208e71d8c473499015371b711d94f1bbb8.png

2. 3D Graph#

from mpl_toolkits.mplot3d import Axes3D
ax = plt.axes(projection='3d')
ax.scatter(x[:, 0], x[:, 1], x[:,2], c=y)
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x7f6f0bdb36d0>
../../../_images/c772dcf26ef0b3bbba6dc8d45f47465cfa083face06c2b358b3b5176843c95ae.png
f = lambda x, y: np.sin(x) + np.cos(x+y)

X = np.linspace(0, 5, 50)
Y = np.linspace(0, 5, 50)
X, Y = np.meshgrid(X, Y)
Z = f(X, Y)

ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, cmap='plasma')
plt.show()
../../../_images/fba5fe33a6dfd5926ac44cc6ad1670c69a43345314613f583a985b3d49331d56.png

3. Histograms#

x = np.random.randn(1000)

plt.figure(figsize=(12, 3))
plt.subplot(121)
plt.hist(x, bins=10)
plt.title('bins=10')
plt.subplot(122)
plt.hist(x, bins=50)
plt.title('bins= 50')
plt.show()
../../../_images/2eb2370b291e57d37d52d1110ed7bd0e0b188517a8d4cb631396a3f706415df2.png
x = iris.data

plt.hist2d(x[:,0], x[:,1], cmap='Blues')
plt.xlabel('longueur sépal')
plt.ylabel('largeur sépal')
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x7f6f0bae3040>
../../../_images/9e0ea0885bb820a11d46349170910d229d81bc54f10872f56af36409d64a1177.png
# Image histogram 
from scipy import misc
face = misc.face(gray=True)

plt.figure(figsize=(12, 4))
plt.subplot(121)
plt.imshow(face, cmap='gray')
plt.subplot(122)
plt.hist(face.ravel(), bins=255)
plt.show()
../../../_images/2a50d74d097e63555e2dc5eb91aa89af45dc07188152b668576d1233611f2c50.png

4. ContourPlot()#

f = lambda x, y: np.sin(x) + np.cos(x+y)*np.cos(x)

X = np.linspace(0, 5, 50)
Y = np.linspace(0, 5, 50)
X, Y = np.meshgrid(X, Y)
Z = f(X, Y)

plt.contour(X, Y, Z, 20, colors='black')
<matplotlib.contour.QuadContourSet at 0x7f6f09704be0>
../../../_images/d568610eb2245234efd38d269ffe7916690731d11dfe105085b32e45500cc89d.png
plt.contourf(X, Y, Z, 20, cmap='RdGy')
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x7f6f0839be20>
../../../_images/8629e48d445c7284c50501e25b0926618bc862892ec8dff597cb653d35768f48.png

5. Imshow()#

plt.figure(figsize=(12, 3))

# Simple graphique imshow()
X = np.random.randn(50, 50)

plt.subplot(131)
plt.imshow(X)
plt.title('Normally random ')

# Matrice de corrélation des iris
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target


plt.subplot(132)
plt.imshow(np.corrcoef(X.T, y))
plt.title('Iris Correlation')

# Matrice f(X, Y) = sin(X) + cos(Y)
X = np.linspace(0, 5, 100)
Y = np.linspace(0, 5, 100)
X, Y = np.meshgrid(X, Y)

plt.subplot(133)
plt.imshow(f(X, Y))
plt.colorbar()
plt.title('f(x, y) = sin(x) + cos(y)')
Text(0.5, 1.0, 'f(x, y) = sin(x) + cos(y)')
../../../_images/ee9aed9fb559b45bd54ac903651bdbd23d5dbb0aab54a1b8880b253c81d14c72.png