You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly Express分组柱状图间隙与排序异常问题求助

Plotly分组柱状图按日期独立排序及格式异常处理

问题背景

现有一个包含Date、Name、Number三列的DataFrame,包含5个日期(数量随数据提取时间变化),每个日期对应10个Name;Name可重复出现或仅出现一次,Number可正可负。数据已按Date(ascending=True)、Number(ascending=False)排序。

尝试用Plotly Express绘制Y轴为Number、X轴为Date、按Name着色的分组柱状图,要求每个日期内的柱条按Number从大到小排序。使用代码fig = px.bar(df, x="Date", y="Number", color="Name", barmode="group")时,仅首个日期排序正确,后续日期存在柱条间隙,且排序错误(如正数柱条出现在负数之后)。尝试fig.update_layout(yaxis={'categoryorder': 'total ascending'})无效。

进一步排查发现,排序规则由X轴首个日期决定:若Name在首个日期存在但后续日期不存在,后续日期会出现空白;若Name不在首个日期但在后续日期存在,其柱条会排在末尾,即便Number更大。需要实现每个X轴值对应的柱条独立排序。

复现代码(2个日期场景)

import pandas as pd
import plotly.express as px

df = pd.DataFrame({
    "Name": ["Joe", "Tom", "Tim", "Alex", "Ben", "Steve", "Nick", "Alan", "Jack", "George", "Joe", "Tom", "Tim", "Leo", "Alex", "Ben", "Nick", "Alan", "Jack", "George"],
    "Date": (["01-01-2024"] * 10) + (["01-02-2024"] * 10),
    "Number": [0.5, 0.4, 0.3, 0.2, 0.1, -0.1, -0.2, -0.3, -0.4, -0.5, 0.5, 0.4, 0.3, 0.2, 0.1, -0.1, -0.2, -0.3, -0.4, -0.5]    
})

df["Date"] = pd.to_datetime(df["Date"])
df.sort_values(by=["Date", "Number"], ascending=[True, False], inplace=True)

print(df)

fig=px.bar(df, x="Date", y="Number", color="Name", barmode="group")

fig.show()

更新:排序解决后出现格式异常

采用r-beginners提供的代码后,排序和间隙问题解决,但出现柱条重叠、大间隙等格式错误。

输入数据

DateNameNumber
2024-02-19B80.0
2024-02-19C70.0
2024-02-19A40.0
2024-02-19D30.0
2024-02-19E10.0
2024-02-19G-20.0
2024-02-19F-40.0
2024-02-19J-50.0
2024-02-19I-60.0
2024-02-19H-90.0
2024-02-20A140.0
2024-02-20C90.0
2024-02-20B80.0
2024-02-20E40.0
2024-02-20K10.0
2024-02-20F-10.0
2024-02-20G-30.0
2024-02-20I-40.0
2024-02-20H-90.0
2024-02-20J-140.0
2024-02-21C100.0
2024-02-21B90.0
2024-02-21A80.0
2024-02-21D30.0
2024-02-21E20.0
2024-02-21F-20.0
2024-02-21G-40.0
2024-02-21H-100.0
2024-02-21I-130.0
2024-02-21J-150.0
2024-02-22A30.0
2024-02-22E30.0
2024-02-22B20.0
2024-02-22C10.0
2024-02-22D10.0
2024-02-22F-20.0
2024-02-22G-50.0
2024-02-22H-70.0
2024-02-22I-70.0
2024-02-22J-110.0
2024-02-23B170.0
2024-02-23C90.0
2024-02-23E50.0
2024-02-23A10.0
2024-02-23D10.0
2024-02-23F50.0
2024-02-23G-10.0
2024-02-23H-80.0
2024-02-23I-80.0
2024-02-23J-150.0

使用的代码

import plotly.graph_objects as go

fig = go.Figure()

# 补充颜色字典定义,避免代码报错
color_dict = {name: f'hsl({i*36},70%,50%)' for i, name in enumerate(df['Name'].unique())}

for d in df['Date'].unique():
    dff = df.query('Date == @d')
    for n in dff['Name'].unique():
        dfn = dff.query('Name == @n')
        fig.add_trace(go.Bar(
            x=dfn['Date'],
            y=dfn['Number'],
            marker=dict(color=color_dict[n]),
            name=n,
            width=60*60*1000
            )
    )
names = set()
fig.for_each_trace(
    lambda trace:
        trace.update(showlegend=False)
        if (trace.name in names) else names.add(trace.name))

unique_dates = df["Date"].unique()
min_x = unique_dates[0]
max_x = unique_dates[:-1]

fig.update_layout(xaxis_range=[min_x, max_x])
fig.update_layout(height=500, width=800, barmode='group')
    
fig.show()

异常表现

输出图表存在柱条重叠、大间隙等格式异常。


内容的提问来源于stack exchange,提问作者Joe Smart

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 14:49:50