02 MatplotLib
In [ ]:
import matplotlib.pyplot as pltIn [ ]:
import numpy as np
x = np.array([0, 10])
y = np.array([0, 25])
plt.plot(x, y)
plt.show()In [ ]:
plt.plot(x, y, 'o')[<matplotlib.lines.Line2D at 0x7fbcaff72f90>]
In [ ]:
x = np.array([0, 10, 14])
y = np.array([0, 25, 32])
plt.plot(x, y, marker = 'o', ms = 10, mec = 'b', mfc = 'b', linestyle = 'dashed')
plt.show()In [ ]:
y1 = np.array([0, 10, 14])
y2 = np.array([3, 25, 32])
plt.plot(y1)
plt.plot(y2)
plt.show()In [ ]:
y1 = np.array([0, 10, 14])
y2 = np.array([3, 25, 32])
plt.plot(y1)
plt.plot(y2)
plt.xlabel("Etichetta asse X")
plt.ylabel("Etichetta asse Y")
plt.title("Il mio grafico")
plt.grid()
plt.show()In [12]:
y1 = np.array([0, 10, 14])
y2 = np.array([3, 25, 32])
plt.subplot(1, 2, 1)
plt.plot(y1)
plt.subplot(1, 2, 2)
plt.plot(y2)
plt.show()In [14]:
y1 = np.array([0, 10, 14])
y2 = np.array([3, 25, 32])
plt.subplot(2, 1, 1)
plt.plot(y1)
plt.subplot(2, 1, 2)
plt.plot(y2)
plt.show()In [17]:
x = np.array([0, 1, 6, 10, 14])
y = np.array([3, 8, 12, 25, 21])
plt.scatter(x,y)
plt.show()In [22]:
x = np.array([0, 1, 6, 10, 14])
y = np.array([3, 8, 12, 25, 21])
colori = np.array([0, 25, 50, 75, 100])
plt.scatter(x,y, c = colori, cmap='Oranges')
plt.colorbar()
plt.show()In [26]:
x = np.array(["Roma", "Milano", "Napoli", "Torino", "Palermo"])
y = np.array([2.77, 1.37, 0.92, 0.86, 0.63])
plt.bar(x,y, color='green')
plt.xlabel("Città")
plt.ylabel("Popolazione (milioni)")
plt.title("Città italiane più popolate")
plt.show()In [30]:
x = np.array(["Roma", "Milano", "Napoli", "Torino", "Palermo"])
y = np.array([2.77, 1.37, 0.92, 0.86, 0.63])
plt.barh(x,y, color='green')
plt.xlabel("Popolazione (milioni)")
plt.ylabel("Città")
plt.title("Città italiane più popolate")
plt.show()In [32]:
import numpy as np
x = np.random.normal(50, 10, 100)
print(x)
print(x.shape)[46.10459237 52.13284307 55.9432274 68.63741331 45.52922771 55.99876533 52.93606021 47.44452274 56.79950283 45.40349713 49.43944443 56.08917238 35.80222779 47.69082021 45.73232919 49.7722653 32.5112663 53.4440787 51.89920478 64.51596897 55.01859624 51.64173573 52.77336445 49.4129904 40.33291293 38.49076082 49.12610112 42.94624854 45.60831979 49.71804411 43.27750026 63.87751454 39.93038903 71.49379777 60.62698935 46.15719679 51.97980807 39.26925113 48.632626 68.49083057 39.51812604 41.20517766 53.29317929 50.67878328 56.54725693 34.64039672 58.51852133 49.72805366 54.94410731 37.79202577 53.83679589 46.01309401 39.96877642 47.17250134 54.27154196 48.3466194 61.01498306 58.41300918 43.00368166 53.32563832 56.12564616 59.44229136 43.39088352 50.65553568 52.49099058 52.70312768 60.14411786 41.67286424 49.28693801 50.81778159 56.88648612 39.53550447 52.98640342 55.56667509 44.99855164 50.90526301 75.76392 55.35452598 51.76591171 55.77729214 43.45798109 33.24212153 27.04045654 64.10608013 38.03718668 45.71403572 38.92345777 58.08398773 38.41838301 48.470952 50.06039611 63.89054442 53.49393728 54.51238674 39.9599154 37.2392247 60.50104055 49.42035827 61.2652698 47.5452872 ] (100,)
In [33]:
plt.hist(x)
plt.show()In [36]:
etichette = np.array(["Mele", "Pere", "Banane", "Arance"])
y = np.array([20, 15, 12, 6])
plt.pie(y, labels = etichette)
plt.show()