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

KDE图表点显示异常:无法清晰查看且截断,调整dpi/figsize无效求助

KDE图表标记点截断问题解决

问题详情

我在KDE图表上标记了均值、中位数、标准差三个统计点,但图表无法完整显示这些元素,存在截断情况。尝试调整dpi与figsize参数后问题仍未解决。图表底部的均值(红色点)、中位数(绿色点)及标准差线段被部分截断,无法完整查看。

绘图代码

# plotting KDE plot with descriptives
plt.figure(dpi = 256)
sns.kdeplot(churn_age, shade = True)
sns.scatterplot( [mean], [0], color='red', label='mean')
sns.scatterplot( [median], [0], color='green', label='median')
sns.lineplot( stdev, [0,0], color='black', label='standard deviation')
plt.xlabel("Age")
plt.ylabel('Density')
plt.title("KDE Plot with descriptives : Age of people who churned")
plt.show()

解决方法

  • 调整图表边距:在plt.show()前添加plt.margins(y=0.15),通过扩大y轴方向的边距,给底部标记元素留出显示空间,数值可根据实际情况调整。
  • 使用子图对象控制布局:改用plt.subplots()创建画布和子图,直接对子图设置边距,更精准控制布局:
    fig, ax = plt.subplots(figsize=(8, 6), dpi=256)
    sns.kdeplot(churn_age, shade=True, ax=ax)
    sns.scatterplot([mean], [0], color='red', label='mean', ax=ax)
    sns.scatterplot([median], [0], color='green', label='median', ax=ax)
    sns.lineplot(stdev, [0,0], color='black', label='standard deviation', ax=ax)
    
    ax.set_xlabel("Age")
    ax.set_ylabel('Density')
    ax.set_title("KDE Plot with descriptives : Age of people who churned")
    ax.margins(y=0.2)  # 调整y轴底部边距
    plt.show()
    
  • 检查标准差数据:确认stdev是包含两个端点的列表(如[mean - std_value, mean + std_value]),确保线段在x轴可视范围内,避免因数据范围超出图表导致截断。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 11:09:16