![](/img/trans.png)
[英]How to sort x-axis label and legends in python plotly express?
[英]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.bar
的plotly.express
: https : plotly.express
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.