Make bar graph with percentage in Plotly

Viewed 640

I want to put this data by vaccine type in a single bar in Plotly, showing the percentage that each vaccine represents from the total, but I am not able to do it.

import plotly.express as px

grafico_dose1 = px.bar(dose1_perc, x="percentual (%)", y=dose1_perc.index, color=dose1_perc.index)
grafico_dose1.show()

enter image description here

1 Answers
  • without reshaping your data frame
  • make it a stacked horizontal bar, making yaxis a constant
import pandas as pd
import numpy as np
import plotly.express as px

dose1_perc = pd.DataFrame({"vacina_nome":["AstraZeneca","Coronavac","Pfizer"], "Percentual (%)":[43.0,25.0,32.0]}).set_index("vacina_nome")

px.bar(dose1_perc, y=np.full(len(dose1_perc), "first dose"), x="Percentual (%)", color=dose1_perc.index, orientation="h")

enter image description here

Related