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

为何Seaborn lineplot中存在数据点缺失情况?

问题描述

我尝试使用Seaborn绘制lineplot,发现生成的图表缺失了部分数据点,导致展示结果不准确。所用代码如下:

import matplotlib.pyplot as plt
import seaborn as sns

x = [0.0, 0.0, 0.0, 0.0, 0.0, 0.2, 0.6, 0.8, 1.0]
y = [0.0, 0.21, 0.41, 0.81, 1.0, 1.0, 1.0, 1.0, 1.0]

ax = sns.lineplot(x = x, y = y)
sns.scatterplot(x = x, y = y, ax = ax)
plt.show()

我已尝试Seaborn 0.11.2和0.12.2版本,该问题仍存在,请问原因是什么?

问题原因及解决方法

Seaborn的lineplot默认会对相同x值的y数据进行聚合计算(默认取均值),这就是x=0.0位置只显示一个聚合后y值的原因,而非连接所有原始数据点。

要保留所有原始数据点的连线,需修改lineplot参数:

  • 设置estimator=None关闭聚合行为
  • 设置sort=False保持原始数据顺序

修改后的代码:

import matplotlib.pyplot as plt
import seaborn as sns

x = [0.0, 0.0, 0.0, 0.0, 0.0, 0.2, 0.6, 0.8, 1.0]
y = [0.0, 0.21, 0.41, 0.81, 1.0, 1.0, 1.0, 1.0, 1.0]

ax = sns.lineplot(x=x, y=y, estimator=None, sort=False)
sns.scatterplot(x=x, y=y, ax=ax)
plt.show()

这样修改后,lineplot会按原始数据顺序连接所有点,不会丢失数据点。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 09:10:37