5.6 Matplotlib Proporzioni
In [1]:
import numpy as np
import matplotlib.pyplot as plt
etichette = np.array(["Mele", "Pere", "Banane", "Arance"])
y = np.array([20, 15, 12, 6])
plt.rcParams['figure.figsize'] = [6, 3]
plt.pie(y, labels = etichette, autopct='%1.1f%%')
plt.show()In [2]:
plt.pie(y, labels = etichette, autopct='%1.1f%%')
# aggiungo un cerchio bianco al centro
my_circle=plt.Circle( (0,0), 0.6, color='white')
p=plt.gcf()
p.gca().add_artist(my_circle)
plt.show()In [3]:
etichette_dett = np.array(["Mele Gialle", "Mele Rosse", "Pere Verdi", "Pere Gialle", "Banane Mature", "Banane Acerbe", "Arance", "Arance Rosse"])
y_dett = np.array([8, 12, 5, 10, 6, 6, 5,1])
# preparo colori della stessa palette per la stessa categoria
a,b,c,d = [plt.cm.winter, plt.cm.cool, plt.cm.spring, plt.cm.copper]
outer_colors = [a(.5), b(.5), c(.5), d(.5)]
inner_colors = [a(.6), a(.4),
b(.6), b(.4),
c(.6), c(.4),
d(.6), d(.4)]
size = 0.3
fig, ax = plt.subplots(figsize=(10,6))
ax.pie(y, radius=1, labels=etichette, colors=outer_colors, wedgeprops=dict(width=size, edgecolor='w'))
ax.pie(y_dett, radius=1-size, labels=etichette_dett, colors=inner_colors, wedgeprops=dict(width=size, edgecolor='w'), labeldistance=0.7, textprops={'fontsize': 8})
plt.plot()[]
In [4]:
x = np.array(['Roma', 'Milano', 'Napoli', 'Torino', 'Palermo'])
y = np.array([2.77, 1.37, 0.92, 0.86, 0.63])
y1 = np.array([68, 51, 73, 42, 88])
y2 = 100-y1
plt.bar(x, y1, label = 'Uomini')
plt.bar(x, y2, bottom=y1, label = 'Donne')
plt.legend(loc = 'lower right')
plt.show()In [5]:
import squarify as sq
sq.plot(sizes = y_dett, label = etichette_dett, alpha=.8 )
plt.show()