-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdengue_serotipos.py
161 lines (139 loc) · 4.22 KB
/
dengue_serotipos.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import pandas as pd
import plotly.graph_objects as go
from plotly.colors import qualitative
from plotly.subplots import make_subplots
SEROTIPOS = {
1: "DENV-1",
2: "DENV-2",
3: "DENV-3",
4: "DENV-4",
5: "Sin serotipo aislado",
}
def main(año):
"""
Crea gráficas de dona con los casos confirmados y
defunciones por dengue desagregadas por serotipo.
Parameters
----------
año: int
El año que se desea graficar.
"""
# Cargamos el dataset de dengue del año que nos interesa.
df = pd.read_csv(f"./data/{año}.csv")
# Seleccionamos los casos confirmados y agrupamos por serotipo.
casos = (
df[df["ESTATUS_CASO"] == 2]["RESULTADO_PCR"]
.value_counts()
.to_frame("total")
.sort_index()
)
casos.index = casos.index.map(SEROTIPOS)
casos["perc"] = casos["total"] / casos["total"].sum() * 100
casos["texto"] = casos.apply(
lambda x: f"<b>{x['perc']:,.2f}%</b><br>({x['total']:,.0f})", axis=1
)
# Seleccionamos las defunciones y agrupamos por serotipo.
defunciones = (
df[df["DICTAMEN"] == 1]["RESULTADO_PCR"]
.value_counts()
.to_frame("total")
.sort_index()
)
defunciones.index = defunciones.index.map(SEROTIPOS)
defunciones["perc"] = defunciones["total"] / defunciones["total"].sum() * 100
defunciones["texto"] = defunciones.apply(
lambda x: f"<b>{x['perc']:,.2f}%</b><br>({x['total']:,.0f})", axis=1
)
# Definimos los títulos para cada gráfica de dona.
titulos = [
f"<b>{casos['total'].sum():,}</b><br>Casos",
f"<b>{defunciones['total'].sum():,}</b><br>Defs.",
]
# Crearemos dos gráficas de dona, una para casos confirmados y una para defunciones.
fig = make_subplots(
rows=1,
cols=2,
horizontal_spacing=0.12,
subplot_titles=titulos,
specs=[[{"type": "pie"}, {"type": "pie"}]],
)
fig.add_trace(
go.Pie(
labels=casos.index,
values=casos["total"],
text=casos["texto"],
texttemplate="%{text}",
hole=0.75,
textposition="outside",
marker_line_color="#041C32",
marker_line_width=5,
sort=False,
),
row=1,
col=1,
)
fig.add_trace(
go.Pie(
labels=defunciones.index,
values=defunciones["total"],
text=defunciones["texto"],
texttemplate="%{text}",
hole=0.75,
textposition="outside",
marker_line_color="#041C32",
marker_line_width=5,
sort=False,
),
row=1,
col=2,
)
fig.update_layout(
colorway=qualitative.Vivid,
legend_orientation="h",
showlegend=True,
legend_itemsizing="constant",
legend_x=0.5,
legend_y=-0.15,
legend_xanchor="center",
legend_yanchor="top",
width=1280,
height=720,
font_family="Quicksand",
font_color="#FFFFFF",
font_size=18,
title_text=f"Casos confirmados y defunciones por dengue en México durante el {año} por serotipo",
title_x=0.5,
title_y=0.95,
margin_t=130,
margin_l=40,
margin_r=40,
margin_b=120,
title_font_size=26,
paper_bgcolor="#041C32",
)
# Ajustamos la posición y el tamaño de los títulos de cada gráfica.
for annotation in fig["layout"]["annotations"]:
annotation["y"] = 0.5
annotation["yanchor"] = "middle"
annotation["font"]["size"] = 70
fig.add_annotation(
x=-0.05,
xanchor="left",
xref="paper",
y=-0.28,
yanchor="bottom",
yref="paper",
text="Fuente: SSA (03/01/2024)",
)
fig.add_annotation(
x=1.05,
xanchor="right",
xref="paper",
y=-0.28,
yanchor="bottom",
yref="paper",
text="🧁 @lapanquecita",
)
fig.write_image(f"./serotipos_{año}.png")
if __name__ == "__main__":
main(2023)