[英]Sorting grouped bar plots dependently in Plotly
我有 3 個 x 值(分組條形圖),每行按名稱分隔(下面的 plot),使用fig.update_xaxes(categoryorder='category ascending')
顯示。
這些值取決於名稱列。 甲乙,C。
它總是根據A值排序,我想根據B和C獨立排序。 (按最大的紅色排序,第五個紅色值應該排在第一位,然后按最大的棕色排序)。
我怎樣才能做到這一點而不重復自己?
完整的可重現代碼:
想先按Weight
對玩家進行排序,然后按Height
排序,最后按Score
排序。
像這樣:
首先按權Weight
排序:[Dante, Evan, Arthur, Carlos, Frank, Bneson]
第二種按Height
排序:[Arthur, Frank, Dante, Carlos, Evan, Benson]...
import pandas as pd
import numpy as np
import plotly.graph_objects as go
dftst = pd.DataFrame([('Arthur', 110, 200, 10),
('Benson', 50, 160, 12),
('Carlos', 90, 170, 14),
('Dante', 160, 180, 11),
('Evan', 130, 165, 18),
('Frank', 70, 190, 20)],
columns=('Name', 'Weight', 'Height', 'Score'))
pnames = dftst['Name']
x = list(range(len(pnames)))
bar_plots = [
go.Bar(x=x, y=dftst['Weight'], name='Weight', marker=go.bar.Marker(color='#0343df')),
go.Bar(x=x, y=dftst['Height'], name='Height', marker=go.bar.Marker(color='#e50000')),
go.Bar(x=x, y=dftst['Score'], name='Score', marker=go.bar.Marker(color='green')),
]
layout = go.Layout(
title=go.layout.Title(text="Players", x=0.5),
yaxis_title="Players Details",
xaxis_tickmode="array",
xaxis_tickvals=list(range(6)),
xaxis_ticktext=tuple(dftst['Name'].values))
fig = go.Figure(data=bar_plots, layout=layout)
fig.update_layout(
autosize=False,
width=600,
height=700)
fig.update_layout(barmode='group', xaxis={'categoryorder':'category ascending'})
fig.update_xaxes(
showgrid=True,
ticks="outside",
tickson="boundaries",
ticklen=20
)
fig.show()
import pandas as pd
import numpy as np
import plotly.express as px
df = pd.DataFrame({c: np.random.randint(1, 100, 40) for c in "ABC"})
fig = px.bar(y=[df[c].sort_values(ascending=0) for c in df.columns],barmode="group")
for t, c in zip(fig.data, df.columns):
t.update(name=c)
fig
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.