slides - clic

Transcript

slides - clic
03/05/2016 TEORIE E TECNICHE DEL RICONOSCIMENTO
Matplotlib
Matplotlib •  Probabilmente la libreria piu’ popolare per visualizzazione 2D in Python 1 03/05/2016 1. Rappresentare funzioni come curve Esempio base di pyplot import numpy as np!
import matplotlib.pyplot as plt!
!
X = np.linspace(-np.pi, np.pi, 256, endpoint=True)!
C,S = np.cos(X), np.sin(X)!
!
plt.plot(X,C)!
plt.plot(X,S)!
!
plt.show()!
2 03/05/2016 pyplot I parametri •  L’esempio e’ ripetuto nello script plot_parameters.py, ma provvedendo esplicitamente i valori di default per tuN i parametri cosi’ potete sperimentare 3 03/05/2016 Esempio: cambiare colore / larghezza plt.figure(figsize=(10,6), dpi=80) !
plt.plot(X, C, color="blue",
linewidth=2.5, linestyle="-")!
plt.plot(X, S, color="red",
linewidth=2.5, linestyle="-")!
MolR altri esempi: hSp://www.labri.fr/perso/nrougier/teaching/matplotlib/ 2. ScaSerplots 4 03/05/2016 ScaSerplots: base import numpy as np!
import maplotlib.pyplot as plt !
n = 1024 !
X = np.random.normal(0,1,n) !
Y = np.random.normal(0,1,n) !
plt.scatter(X,Y) !
plt.show()!
ScaSerplots: versione piu’ sofisRcata scatter_ex.py!
5 03/05/2016 3. Bar plots Bar plots: base import numpy as np import maplotlib.pyplot as plt n = 12 X = np.arange(n) Y1 = (1-­‐X/float(n)) * np.random.uniform(0.5,1.0,n) Y2 = (1-­‐X/float(n)) * np.random.uniform(0.5,1.0,n) plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white') plt.bar(X, -­‐Y2, facecolor='#ff9999', edgecolor='white') for x,y in zip(X,Y1): plt.text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'boSom') plt.ylim(-­‐1.25,+1.25) plt.show()!
6 03/05/2016 4. Pie charts Pie charts: base import numpy as np !
import maplotlib.pyplot as plt !
n = 20 !
Z = np.random.uniform(0,1,n) !
plt.pie(Z) !
plt.show()!
7 03/05/2016 Pie charts: versione piu’ sofisRcata pie_ex.py!
5. Griglie (grids) 8 03/05/2016 Griglie: base import numpy as np !
import maplotlib.pyplot as plt !
!
axes = plt.gca() !
axes.set_xlim(0,4) !
axes.set_ylim(0,3) !
axes.set_xticklabels([]) !
axes.set_yticklabels([]) !
plt.show()!
Griglie: versione piu’ sofisRcata grid_ex.py!
9 03/05/2016 6. Diagrammi mulRpli (mulR-­‐plots) MulR-­‐plots: base import numpy as np !
import maplotlib.pyplot as plt !
!
plt.subplot(2,2,1) !
plt.subplot(2,2,3) !
plt.subplot(2,2,4) !
plt.show()!
subplot(nrows, ncols, plot_number)!
10 03/05/2016 MulR-­‐plots: versione piu’ sofisRcata multiplot_ex.py!
7. 3D 11 03/05/2016 3D: base import numpy as np import maplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig) X = np.arange(-­‐4, 4, 0.25) Y = np.arange(-­‐4, 4, 0.25) X, Y = np.meshgrid(X, Y) R = np.sqrt(X**2 + Y**2) Z = np.sin(R) ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot') plt.show()!
8. Testo 12 03/05/2016 Testo: codice text_ex.py!
Per molR altri esempi •  hSp://matplotlib.org/users/
pyplot_tutorial.html •  hSp://www.labri.fr/perso/nrougier/teaching/
matplotlib/ 13