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

Jupyter环境中Seaborn jointplot的kde模式无法正常运行问题排查

解决Seaborn jointplot中kind="kde"结合hue参数时的NameError问题

我之前也碰到过一模一样的问题,你遇到的这个NameError: name 'kdeplot' is not defined是Seaborn 0.11.1版本里的一个已知内部bug——当你在jointplot中同时使用kind="kde"和hue参数时,函数内部调用kdeplot的方式存在问题,导致没有正确识别到这个函数,哪怕你代码写法和官方示例一致也会触发这个错误。

解决方案

方案1:升级Seaborn版本(推荐)

这个bug在Seaborn 0.11.2及后续版本中已经被修复,你只需要执行升级命令即可:

pip install --upgrade seaborn

升级完成后重新运行你的代码,应该就能正常生成带hue的双变量KDE联合图了。

方案2:手动使用JointGrid替代(无需升级)

如果暂时无法升级版本,你可以用JointGrid手动搭建相同的可视化效果,绕开jointplot的内部bug:

import seaborn as sns
import matplotlib.pyplot as plt

# 创建JointGrid对象,指定数据、坐标轴和分组变量
g = sns.JointGrid(data=test_df, x="skew", y="vol", hue="periode")
# 绘制中间的联合KDE图
g.plot_joint(sns.kdeplot)
# 绘制两侧的边缘KDE图(fill=True可以填充曲线下的区域)
g.plot_marginals(sns.kdeplot, fill=True)

plt.show()

这样同样能实现你想要的分组双变量KDE可视化效果,而且不会触发那个NameError。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 14:47:36