In questo breve quadernetto vado a realizzare un esempio di come fare un bar plot di due categorie di dati contenute in un dataframe. Prima devo classificare diverse board come funzionanti o difettose, successivamente andare a sommare alcuni indicatori
Esempio realizzato per Cesare, relativo alle attività di Random Power
Inizo caricando i moduli necessari
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
Visto che è la tua richiesta, inizio a caricare il dataset
df = pd.read_csv("setStats.csv")
e lo ispeziono
df.head(10)
boardID | setNum | nistGold | nistSilver | nistBronze | nistDiscarded | nistGoldNorm | nistSilverNorm | nistBronzeNorm | nistDiscardedNorm | countCrush | countAlphabit | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Board#0013 | set_1 | 10 | 5 | 1 | 4 | 0.50 | 0.25 | 0.05 | 0.20 | 10 | 4 |
1 | Board#0013 | set_2 | 13 | 6 | 0 | 1 | 0.65 | 0.30 | 0.00 | 0.05 | 11 | 2 |
2 | Board#0013 | set_3 | 14 | 4 | 1 | 1 | 0.70 | 0.20 | 0.05 | 0.05 | 13 | 2 |
3 | Board#46845 | set_1 | 9 | 8 | 2 | 1 | 0.45 | 0.40 | 0.10 | 0.05 | 2 | 0 |
4 | Board#46845 | set_2 | 11 | 5 | 2 | 2 | 0.55 | 0.25 | 0.10 | 0.10 | 4 | 0 |
5 | Board#46845 | set_3 | 10 | 9 | 0 | 1 | 0.50 | 0.45 | 0.00 | 0.05 | 0 | 0 |
6 | Board#46845 | set_4 | 11 | 3 | 2 | 4 | 0.55 | 0.15 | 0.10 | 0.20 | 2 | 0 |
7 | Board#46845 | set_5 | 9 | 6 | 3 | 2 | 0.45 | 0.30 | 0.15 | 0.10 | 3 | 0 |
8 | Board#46845 | set_6 | 10 | 6 | 1 | 3 | 0.50 | 0.30 | 0.05 | 0.15 | 4 | 0 |
9 | Board#46846 | set_1 | 9 | 4 | 2 | 5 | 0.45 | 0.20 | 0.10 | 0.25 | 2 | 0 |
Definisco la lista delle board malate. Qui sto facendo l'assunzione che i due insiemi siano disgiunti e che non esistano board fuori. In caso esistessero devi fare un lavoro simile ma tenendo traccia di n possibili popolazioni
fboards = ['Board#46845', 'Board#46846', 'Board#46847', 'Board#46848', 'Board#46852', 'Board#46855']
dboards = ['Board#0013', 'Board#46851', 'Board#46853', 'Board#46856']
df.boardID.isin(fboards).head(10)
0 False 1 False 2 False 3 True 4 True 5 True 6 True 7 True 8 True 9 True Name: boardID, dtype: bool
Visto che vogliamo lavorare con i dataframe, aggiungo una colonna al dataframe che mi dice quali schede sono funzionanti
#Vale True per le fboards, assumo che le altre siano dboards
df["funziona"] = df.boardID.isin(fboards)
df.head(10)
boardID | setNum | nistGold | nistSilver | nistBronze | nistDiscarded | nistGoldNorm | nistSilverNorm | nistBronzeNorm | nistDiscardedNorm | countCrush | countAlphabit | funziona | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Board#0013 | set_1 | 10 | 5 | 1 | 4 | 0.50 | 0.25 | 0.05 | 0.20 | 10 | 4 | False |
1 | Board#0013 | set_2 | 13 | 6 | 0 | 1 | 0.65 | 0.30 | 0.00 | 0.05 | 11 | 2 | False |
2 | Board#0013 | set_3 | 14 | 4 | 1 | 1 | 0.70 | 0.20 | 0.05 | 0.05 | 13 | 2 | False |
3 | Board#46845 | set_1 | 9 | 8 | 2 | 1 | 0.45 | 0.40 | 0.10 | 0.05 | 2 | 0 | True |
4 | Board#46845 | set_2 | 11 | 5 | 2 | 2 | 0.55 | 0.25 | 0.10 | 0.10 | 4 | 0 | True |
5 | Board#46845 | set_3 | 10 | 9 | 0 | 1 | 0.50 | 0.45 | 0.00 | 0.05 | 0 | 0 | True |
6 | Board#46845 | set_4 | 11 | 3 | 2 | 4 | 0.55 | 0.15 | 0.10 | 0.20 | 2 | 0 | True |
7 | Board#46845 | set_5 | 9 | 6 | 3 | 2 | 0.45 | 0.30 | 0.15 | 0.10 | 3 | 0 | True |
8 | Board#46845 | set_6 | 10 | 6 | 1 | 3 | 0.50 | 0.30 | 0.05 | 0.15 | 4 | 0 | True |
9 | Board#46846 | set_1 | 9 | 4 | 2 | 5 | 0.45 | 0.20 | 0.10 | 0.25 | 2 | 0 | True |
Vado ad effettuare le somme per ciascuna cosa di interesse
myGold = df.groupby("funziona")["nistGold"].sum()
myGold
funziona False 259 True 382 Name: nistGold, dtype: int64
mySilver = df.groupby("funziona")["nistSilver"].sum()
mySilver
funziona False 134 True 180 Name: nistSilver, dtype: int64
myBronze = df.groupby("funziona")["nistBronze"].sum()
myBronze
funziona False 27 True 45 Name: nistBronze, dtype: int64
Per essere sicuro controllo che tipo sono questi oggetti
type(myGold)
pandas.core.series.Series
myBronze[1]
45
lstFunzionanti = (myBronze[0], mySilver[0], myGold[0])
lstDifettose = (myBronze[1], mySilver[1], myGold[1])
lstNomi = ("Bronzo", "Argento", "Oro")
print(lstFunzionanti)
print(lstDifettose)
(27, 134, 259) (45, 180, 382)
Procedo al plot. cfr
x = np.arange(len(lstNomi)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, lstFunzionanti, width, label = "Funzionanti")
rects2 = ax.bar(x + width/2, lstDifettose, width, label = "Difettose")
ax.set_ylabel("Categoria")
ax.set_title("Somma")
plt.xticks(x, lstNomi) # Gives a strange error...
ax.legend()
ax.set_ylim(0, 450)
#ax.grid(True)
ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)
fig.tight_layout()
plt.show()
fig, ax = plt.subplots()
#fig.set_size_inches(12,5)
x = np.arange(len(lstNomi)) # the label locations
width = 0.1 # the width of the bars
ax.bar(x - width/2, lstFunzionanti, label = "Funzionanti")
ax.bar(x + width/2, lstDifettose, label = "Difettose")
ax.set_ylabel("Categoria")
ax.set_title("Somma")
#ax.set_xticks(x, lstNomi)
ax.legend()
ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)
plt.show()