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

使用Plotly Express绘制时序图异常:多条折线问题排查

Plotly Express绘制时序图出现异常多条折线的解决方法

问题描述

使用Plotly Express绘制时序图时,执行代码fig = px.line(new_df, x='Data', y='quantity')后,生成的图表出现多条折线,不符合预期。但使用Seaborn的sns.lineplot(x='Data', y='quantity', hue='Year', data=new_df)可正确绘制目标图表。

数据集信息

new_df.info()

<class 'pandas.core.frame.DataFrame'>
Int64Index: 59 entries, 13 to 9686
Data columns (total 3 columns):
 #   Column                     Non-Null Count  Dtype         
---  ------                     --------------  -----         
 0   Data                       59 non-null     datetime64[ns]
 1   quantity                   59 non-null     int64         
 2   Year                       59 non-null     int64         
dtypes: datetime64[ns](1), int64(2)
memory usage: 1.8 KB

问题原因

Seaborn的sns.lineplot通过hue='Year'明确指定按Year列分组绘制折线;而Plotly Express的px.line在未指定分组参数时,会自动基于数据中的离散列(此处Year为离散列)进行分组,但受数据日期结构特性影响,自动分组逻辑生成了不符合预期的多条折线。

解决方案

显式指定color='Year'参数,让Plotly Express按年份分组绘制折线,与Seaborn的hue效果一致:

fig = px.line(new_df, x='Data', y='quantity', color='Year')

内容的提问来源于stack exchange,提问作者Надин Л.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 21:42:42