Repository navigation
Expand file tree
/
Copy pathplot_stats_short.py
More file actions
124 lines (98 loc) · 4.51 KB
/
Copy pathplot_stats_short.py
File metadata and controls
124 lines (98 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as ticker
# Load CSV file with no index column
df = pd.read_csv("risposte_questionario_5_short.csv", index_col=False)
# Reset the index just in case
df = df.reset_index(drop=True)
# Seleziona le colonne di interesse (1, 2, 3, 4)
selected_questions = ["1", "2", "3", "4"]
# Assicurati che le colonne selezionate siano trattate come dati
df_selected = df[selected_questions]
# Converti tutte le colonne in stringa per evitare problemi con i dati non numerici
df_selected = df_selected.astype(str)
# Determina i valori univoci per ogni domanda
unique_values_per_question = [sorted(df_selected[question].unique()) for question in selected_questions]
# Crea un DataFrame per memorizzare i conteggi
all_unique_values = pd.Index([val for sublist in unique_values_per_question for val in sublist], name='Values').unique()
counts_df = pd.DataFrame(index=all_unique_values, columns=selected_questions).fillna(0)
# Popola counts_df con i conteggi per ciascun valore
for question in selected_questions:
counts = df_selected[question].value_counts()
counts_df.loc[counts.index, question] = counts
# Normalizza i conteggi per ogni domanda (a 19, dato che abbiamo 19 risposte in totale)
counts_df = counts_df.div(counts_df.sum(axis=0), axis=1) * 19
# Definisci i colori per i valori unici
color_map = {
'Female': '#003049',
'Male': '#d62828',
'Pr.': '#003049',
'R.F.': '#d62828',
'Ph.D. Student': '#f77f00',
'Student': '#fcbf49',
'High': '#003049',
'Medium': '#d62828',
'Low': '#f77f00',
'N.A.': '#fcbf49'
}
# Definisci l'ordine dei segmenti per ogni domanda
order_map = {
"1": ["Female", "Male"],
"2": ["Pr.", "R.F.", "Ph.D. Student", "Student"],
"3": ["High", "Medium", "Low", "N.A."],
"4": ["High", "Medium", "Low", "N.A."],
}
# Crea il grafico a barre impilate orizzontali
fig, ax = plt.subplots(figsize=(6, len(selected_questions) * 0.7))
# Inizializza la posizione inferiore per impilare le barre (inizia a 0 per tutte le domande)
bottom = np.zeros(len(selected_questions))
# Plotta ogni valore unico con barre impilate
for j, question in enumerate(selected_questions):
# Ottieni l'ordine specificato per la domanda corrente
ordered_values = order_map.get(question, counts_df.index)
print(counts_df)
for value in ordered_values:
if value in counts_df.index:
width = counts_df.loc[value, question]
print(width)
if width > 0:
bar = ax.barh(
y=f'Q{int(j)+1}', # Etichetta ogni barra con il nome della domanda
width=width,
left=bottom[j],
color=color_map.get(value, '#cccccc'), # Colore specificato per ogni valore unico, con default se non trovato
edgecolor='black', # Bordi neri
height=0.65, # Altezza della barra
zorder=3,
)
# Aggiungi il valore assunto al centro della barra
ax.text(bottom[j] + width / 2, f'Q{int(j)+1}', value, va='center', ha='center', color='white', fontsize=9, fontweight='bold')
# Aggiorna la posizione inferiore per impilare la prossima barra
bottom[j] += width
# Imposta i limiti dell'asse x per evitare spazi bianchi dopo il 100%
ax.set_xlim(0, 19)
# Imposta i tick dell'asse x ai valori da 1 a 19
ax.set_xticks(np.arange(1, 20))
# Imposta le etichette dei tick dell'asse x
ax.set_xticklabels(np.arange(1, 20))
# Riduci la dimensione dei tick delle y e delle x
ax.tick_params(axis='y', labelsize=10)
ax.tick_params(axis='x', labelsize=10)
# Aggiungi griglia e sottogriglia
ax.grid(True, which='major', axis='x', linestyle='--', color='grey', alpha=0.7, zorder=0) # Griglia principale
#ax.grid(True, which='minor', axis='x', linestyle=':', color='lightgrey', alpha=0.7, zorder=0) # Sottogriglia
# Attiva i minor ticks solo sull'asse x
#ax.xaxis.set_minor_locator(ticker.AutoMinorLocator(2)) # Numero di minor ticks
# Imposta le etichette dell'asse y
ax.set_yticks([f'Q{int(j)+1}' for j in range(len(selected_questions))])
ax.set_yticklabels([f'Q{int(j)+1}' for j in range(len(selected_questions))])
# Inverti l'asse y
ax.invert_yaxis()
# Aggiungi etichette e titolo
ax.set_xlabel('Number of Responses', fontsize=10) # Etichetta asse x
ax.set_ylabel('Question ID', fontsize=12) # Etichetta asse y
plt.tight_layout()
plt.savefig("stacked_barchart_final.pdf", bbox_inches='tight')
# Mostra il grafico
plt.show()