【问题标题】:Make bar graph with percentage in Plotly在 Plotly 中制作带有百分比的条形图
【发布时间】:2021-11-11 18:08:14
【问题描述】:

我想按疫苗类型将这些数据放在 Plotly 的单个条形图中,显示每种疫苗占总数的百分比,但我做不到。

import plotly.express as px

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

【问题讨论】:

    标签: database graph plotly percentage


    【解决方案1】:
    • 无需重塑数据框
    • 使其成为堆叠的水平条,使 yaxis 成为常数
    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")
    

    【讨论】:

    • 非常感谢。完美运行!
    猜你喜欢
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多