Utilisation des librairies numpy et matplotlib¶
- numpy est une librairie permettant de simplifier la manipulation des matrices
- matplotlib est une librairie permettant de dessiner des graphiques
- Elles sont installées avec pip:
pip install matplotlib numpy
numpy¶
np.zeros
permet de générer des zéros
In [1]:
Copied!
import numpy as np
import pandas as pd
l1 = np.zeros(5)
print("l1", l1)
l2 = np.zeros((2, 5))
pd.DataFrame(l2)
import numpy as np
import pandas as pd
l1 = np.zeros(5)
print("l1", l1)
l2 = np.zeros((2, 5))
pd.DataFrame(l2)
l1 [0. 0. 0. 0. 0.]
Out[1]:
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
1 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
np.random.choice
permet de générer des données aléatoirement avec une proba
In [2]:
Copied!
import numpy as np
import pandas as pd
N = 10
p = 0.25
vals = ["A", "B", "C", "D"]
items = np.random.choice(vals, N, p=[p, p, p, p])
print(items)
import numpy as np
import pandas as pd
N = 10
p = 0.25
vals = ["A", "B", "C", "D"]
items = np.random.choice(vals, N, p=[p, p, p, p])
print(items)
['B' 'D' 'C' 'A' 'A' 'A' 'D' 'A' 'A' 'B']
In [3]:
Copied!
import numpy as np
import pandas as pd
N = 4
p = 0.8
vals = [False, True]
grid = np.random.choice(vals, N*N, p=[1-p, p]).reshape(N, N)
pd.DataFrame(grid)
import numpy as np
import pandas as pd
N = 4
p = 0.8
vals = [False, True]
grid = np.random.choice(vals, N*N, p=[1-p, p]).reshape(N, N)
pd.DataFrame(grid)
Out[3]:
0 | 1 | 2 | 3 | |
---|---|---|---|---|
0 | True | True | False | True |
1 | True | True | True | False |
2 | True | False | False | True |
3 | True | True | True | True |
matplotlib¶
- Une des fonctions de matplotlib est de dessiner une matrice
In [4]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
N = 4
p = 0.8
vals = [False, True]
grid = np.random.choice(vals, N*N, p=[1-p, p]).reshape(N, N)
fig, ax = plt.subplots()
# Remplacer grid par votre matrice
mat = ax.matshow(grid)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
N = 4
p = 0.8
vals = [False, True]
grid = np.random.choice(vals, N*N, p=[1-p, p]).reshape(N, N)
fig, ax = plt.subplots()
# Remplacer grid par votre matrice
mat = ax.matshow(grid)
plt.show()
ListedColormap
permet de changer la couleur des cellules
from matplotlib.colors import ListedColormap
cmap = ListedColormap(['k', 'w', 'r'])
# ou en RGB ListedColormap([[0, 0, 0], [1, 1, 1], [1, 0, 0]])
cax = ax.matshow(x,cmap=cmap)
In [5]:
Copied!
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
N = 4
p = 0.6
vals = [0, 1, 2]
# [0.5, 0.2, 0.2] -> rgb. w -> white, b -> blue
cmap = ListedColormap([[0.5, 0.2, 0.2], 'w', 'b'])
grid = np.random.choice(vals, N*N, p=[1-p-0.2, p, 0.2]).reshape(N, N)
print(pd.DataFrame(grid))
fig, ax = plt.subplots()
mat = ax.matshow(grid, cmap = cmap)
plt.show()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
N = 4
p = 0.6
vals = [0, 1, 2]
# [0.5, 0.2, 0.2] -> rgb. w -> white, b -> blue
cmap = ListedColormap([[0.5, 0.2, 0.2], 'w', 'b'])
grid = np.random.choice(vals, N*N, p=[1-p-0.2, p, 0.2]).reshape(N, N)
print(pd.DataFrame(grid))
fig, ax = plt.subplots()
mat = ax.matshow(grid, cmap = cmap)
plt.show()
0 1 2 3 0 1 1 1 0 1 1 2 1 1 2 2 2 2 2 3 1 0 1 2
- La composante
animation
dematplotlib
permet de mettre à jour le graphique. - Il faut appeler la méthode
animation.FuncAnimation(fig, update, interval=50, save_count=50)
.- update: fonction à définir qui retourne les nouvelles valeurs de la matrice.
- ⚠: Les animations ne fonctionnent pas sur un navigateuer.
In [6]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
N = 4
p = 0.8
vals = [False, True]
grid = np.random.choice(vals, N*N, p=[1-p, p]).reshape(N, N)
fig, ax = plt.subplots()
mat = ax.matshow(grid)
def update(frame):
grid = np.random.choice(vals, N*N, p=[1-p, p]).reshape(N, N)
mat.set_data(grid)
ani = animation.FuncAnimation(fig, update, interval=100, save_count=50)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
N = 4
p = 0.8
vals = [False, True]
grid = np.random.choice(vals, N*N, p=[1-p, p]).reshape(N, N)
fig, ax = plt.subplots()
mat = ax.matshow(grid)
def update(frame):
grid = np.random.choice(vals, N*N, p=[1-p, p]).reshape(N, N)
mat.set_data(grid)
ani = animation.FuncAnimation(fig, update, interval=100, save_count=50)
plt.show()