Skip to content
Advertisement

How to show data by day in a Plotly chart

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.

enter image description here

grafico = px.histogram(df.query('dose_vacina == "1ª Dose"'), x = "data_aplicacao_vacina", y = "doses_totais", color = "nome_fabricante_vacina",
                         color_discrete_map={
                             "AstraZeneca": "#00CC96",
                             "Coronavac": "#EF553B",
                             "Pfizer": "#AB63FA"},
                         title="Aplicação diária da 1ª dose, em Alagoas",
                         hover_name="nome_fabricante_vacina", hover_data=["nome_fabricante_vacina"])

grafico.show()

Graphic

Advertisement

Answer

Solution 1

You can manually set the width of the bins:

grafico.update_traces(xbins_size=1)

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

days = df['data_aplicacao_vacina'].unique().size

grafico = px.histogram(df.query('dose_vacina == "1ª Dose"'), x = "data_aplicacao_vacina", y = "doses_totais", color = "nome_fabricante_vacina",
                         color_discrete_map={
                             "AstraZeneca": "#00CC96",
                             "Coronavac": "#EF553B",
                             "Pfizer": "#AB63FA"},
                         title="Aplicação diária da 1ª dose, em Alagoas",
                         hover_name="nome_fabricante_vacina", hover_data=["nome_fabricante_vacina"],
                         nbins=days)

grafico.show()

See #Choosing the number of bins for reference.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement