繁体   English   中英

在plotly python中将图例显示为条形图的x轴标签

[英]Show legends as x axis labels of bar charts in plotly python

我正在绘制一个组条形图,其中我在条形图中映射了多行。 这是解释我所做的事情的代码:

data = [{"Project":"Project A","Features":{"AC":95,"Elec":130, "Area":2349.46, "Cars":30, "Rent":2345.00},"ScaledFeatures":{"AC":95,"Elec":130, "Area":2349.46, "Cars":30, "Rent":2345.00}},
{"Project":"Project B","Features":{"AC":95,"Elec":130, "Area":2120.00, "Cars":42, "Rent":5432},"ScaledFeatures":{"AC":95,"Elec":130, "Area":2120.00, "Cars":42, "Rent":2345}}
       ]
featureKeys = list(data[0]["Features"].keys())

for key in featureKeys:
    featureData = ([d["ScaledFeatures"][key] for d in data])
    minimumFeatureValue = min(featureData)
    for d in data:
        d["ScaledFeatures"][key] = d["ScaledFeatures"][key]/minimumFeatureValue

barData = []
for d in data:
    barData.append(go.Bar(name=d['Project'], x=featureKeys, y=list(d["ScaledFeatures"].values()),text=list(d["Features"].values()),textposition='auto'))

# set plot layout  
layout = go.Layout(
    xaxis={"mirror" : "allticks", 'side': 'top'} # x-axis also at top
)

fig = go.Figure(data=barData,layout=layout)

# Change the bar mode
#fig.update_traces(textposition='outside')
fig.update_layout(barmode='group')
fig.show()

这是它生成的输出:

电流输出

我想从中生成以下类似输出,其中图例在 x 轴中出现:

所需输出

到目前为止,我所做的是使用多个轴,但这会在同一图表中绘制自己的条形图。 任何帮助表示赞赏!

您可以在此处使用多类别 x 轴,如本例所示 但是,您会将 A/B 和 AC/Elec 等放在同一侧。 如果您不想使用它,您可以使用注释https://plot.ly/python/text-and-annotations/#simple-annotation 另外,在这里您可以考虑使用px.barplotly.expresshttps : plotly.express

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM