【发布时间】:2021-11-05 09:52:57
【问题描述】:
我正在尝试
- 创建一个漂亮的图,按 LABEL 排序,然后按每个 LABEL 内的值排序。
- 如果可能,请删除图表底部的标签,因为我在图例中有说明。
图书馆:
from plotly import graph_objs as go
import plotly.express as px
import pandas as pd
我的数据如下所示:
df = pd.DataFrame({'LABEL': ['1', '1', '2', '2', '3', '3', '3', '3'],
'Cat2': ['a', 'b', 'a', 'b', 'c', 'a', 'e', 'f'],
'Value': [3, 2, 1, 4, 1, 3, 4, 1]})
df.sort_values(by=['LABEL', 'Value'], ascending=[True, False],inplace=True)
这是我的尝试:
COLOR_MAP = {str(i): c for i, c in enumerate(px.colors.qualitative.Light24)}
fig = go.Figure()
for i in df['LABEL'].unique():
df_ = df[df['LABEL'] == i]
fig.add_trace(go.Bar(
x=[df_['LABEL'], df_['Cat2']],
y=df_['Value'],
marker=dict(color=COLOR_MAP[i]),
name=f'{i}'))
fig.update_layout(legend_title='Cat1')
fig.update_layout(
xaxis=dict(tickangle=45))
fig.update_layout(xaxis={'categoryorder': 'trace'}) # I tried: 'total descending', 'category descending', 'array'
提前致谢!!
【问题讨论】:
标签: python-3.x plotly