【问题标题】:Change image limits of seaborn kdeplot更改 seaborn kdeplot 的图像限制
【发布时间】:2025-12-10 03:55:01
【问题描述】:

当我制作一个将 shade 参数设置为 True 的 seaborn.kdeplot 时,背景是白色的。如果在更宽的范围(此处为蓝线)上绘制了其他内容,则此背景不会扩展到整个图。如何让 kdeplot “扩展到”整个情节?

(最终,让 kdeplot 的第一个阴影是透明的也可以解决问题)

例子:

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

plt.plot([-3, 3],[0,60])

points = np.random.multivariate_normal([0, 0], [[1, 2], [2, 20]], size=1000)
sns.kdeplot(points, shade=True)

plt.show()

【问题讨论】:

  • 不确定目前是否可行,请随时打开 github 问题

标签: python matplotlib seaborn


【解决方案1】:

有效:

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

points = np.random.multivariate_normal([0, 0], [[1, 2], [2, 20]], size=1000)

plt.plot([-3, 3],[0,60])

ax = sns.kdeplot(points, shade=True)
ax.collections[0].set_alpha(0)

plt.show()

【讨论】:

    【解决方案2】:

    你可以通过使用set_style 参数来做到这一点

    import seaborn as sns
    import numpy as np
    import matplotlib.pyplot as plt
    
    plt.plot([-3, 3],[0,60])
    
    points = np.random.multivariate_normal([0, 0], [[1, 2], [2, 20]], size=1000)
    sns.kdeplot(points)
    sns.set_style("white") # HERE WE SET THE BACKGROUND TO BE WHITE
    
    plt.show()
    

    上面的代码会产生这样的图:

    【讨论】:

    • 谢谢,但这只是因为您的 kdeplot 中没有参数“shade=True”。对这个参数做同样的事情仍然会返回同样的问题(尽管我可能会尝试将背景设置为我的第一个阴影的颜色,这可能不是微不足道的)。
    最近更新 更多