You need to enable JavaScript to run this app. 如何用sns.catplot箱线图对比特定行(临水房产)与全部行的价格中位数
优惠活动
大模型
产品
解决方案
定价
更多

如何用sns.catplot箱线图对比特定行(临水房产)与全部行的价格中位数

解决方案

你原来的代码直接以waterfront布尔字段为x轴,会自动拆分出「临水/非临水」两个分组,不符合对比「临水房产/全部房产」的需求,需要先重构绘图数据集,再生成图表即可:

import pandas as pd
import seaborn as sns

# 构造绘图专用数据集
# 提取临水房产子集,新增分组标签
df_water = df[df['waterfront'] == True].assign(category='临水房产')
# 全量房产新增分组标签
df_all = df.assign(category='全部房产')
# 合并两个子集得到绘图数据
plot_df = pd.concat([df_water, df_all], ignore_index=True)

# 绘制箱线图,可通过order参数调整两个箱体的展示顺序
h = sns.catplot(x="category", y="price", kind="box", data=plot_df, order=['全部房产', '临水房产'])

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.21 00:15:04