简体   繁体   English

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

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

I am working on a group bar chart in plotly where I have mapped multiple rows in bar chart.我正在绘制一个组条形图,其中我在条形图中映射了多行。 Here is the code explaining what I did:这是解释我所做的事情的代码:

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()

Here is the output it generates:这是它生成的输出:

电流输出

I want to generate the following like output from this where legends are coming in x-axis:我想从中生成以下类似输出,其中图例在 x 轴中出现:

所需输出

What I have done till now is to use multiple axes but that draws its own bars n the same chart.到目前为止,我所做的是使用多个轴,但这会在同一图表中绘制自己的条形图。 Any help is appreciated!任何帮助表示赞赏!

You could use multicategory x axes here, as in this example .您可以在此处使用多类别 x 轴,如本例所示 However you would have A/B and AC/Elec etc. together on the same side.但是,您会将 A/B 和 AC/Elec 等放在同一侧。 If you don't want to use this you can use annotations https://plot.ly/python/text-and-annotations/#simple-annotation .如果您不想使用它,您可以使用注释https://plot.ly/python/text-and-annotations/#simple-annotation Also, here you could consider using px.bar from plotly.express : https://plot.ly/python/bar-charts/另外,在这里您可以考虑使用px.barplotly.expresshttps : plotly.express

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

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