I have a dataframe with the number of people vaccinated per day and I’m trying to put this data in a daily bar chart of Plotly, but it aggregates the data every about 13 days. I don’t want the aggregated view this way, I need it to show the bars per day.
Below is a part of the dataframe and the code.
JavaScript
x
11
11
1
grafico = px.histogram(df.query('dose_vacina == "1ª Dose"'), x = "data_aplicacao_vacina", y = "doses_totais", color = "nome_fabricante_vacina",
2
color_discrete_map={
3
"AstraZeneca": "#00CC96",
4
"Coronavac": "#EF553B",
5
"Pfizer": "#AB63FA"},
6
title="Aplicação diária da 1ª dose, em Alagoas",
7
hover_name="nome_fabricante_vacina", hover_data=["nome_fabricante_vacina"])
8
9
grafico.show()
10
11
Advertisement
Answer
Solution 1
You can manually set the width of the bins:
JavaScript
1
2
1
grafico.update_traces(xbins_size=1)
2
Solution 2
You can pass a parameter called nbins. If you set nbins to the number of days, you’ll get one bar per day. You could try
JavaScript
1
13
13
1
days = df['data_aplicacao_vacina'].unique().size
2
3
grafico = px.histogram(df.query('dose_vacina == "1ª Dose"'), x = "data_aplicacao_vacina", y = "doses_totais", color = "nome_fabricante_vacina",
4
color_discrete_map={
5
"AstraZeneca": "#00CC96",
6
"Coronavac": "#EF553B",
7
"Pfizer": "#AB63FA"},
8
title="Aplicação diária da 1ª dose, em Alagoas",
9
hover_name="nome_fabricante_vacina", hover_data=["nome_fabricante_vacina"],
10
nbins=days)
11
12
grafico.show()
13
See #Choosing the number of bins for reference.