[英]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.