Search code examples
databasegraphplotlypercentage

Make bar graph with percentage in Plotly


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


Solution

    • 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