![](/img/trans.png)
[英]add frame and remove background colour and grids using seaborn.kdeplot
[英]Colour the background in seaborn kdeplot (python)
我正在使用 seaborn kernel 密度估计到 plot 概率密度轮廓,如下所示:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
x = np.random.normal(0, 3, 100)
y = np.random.normal(0, 1, 100)
fig, ax = plt.subplots()
ax.scatter(x,y, marker='.')
ax.set_aspect('equal')
ax.set(xlim=(-13,13))
ax.set(ylim=(-8,8))
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
sns.kdeplot(x,y, fill=True, shade_lowest=True, alpha=0.7, linewidths=3, \
cmap='coolwarm', ax=ax, cbar=True, cbar_ax = cax)
colour = ax.collections[1].get_facecolor()
我正在生产其中的许多产品,因此为了比较它们,我希望修复 plot 限制。 如您所见,我的问题是当我更改 plot 的限制时,seaborn 不会填充背景。
我的代码最后一行中的变量colour
包含我想要填充背景的内容。 我需要帮助弄清楚如何做到这一点。 我试过了
ax.set_facecolor(colour.reshape(4))
这个问题与这个有 6 年历史的问题基本相同,该问题提出只删除最后一个轮廓下方的填充。 我相信必须有一种方法来获得所需的行为。 我真的很感激任何帮助!
作为奖励: sns.kdeplot() 的linewidths
参数什么都不做。 如何更改轮廓线的线宽?
正如@mwaskom 在评论中所建议的,您可以使用cut
parameter 。
使用 cut 参数可以部分避免这种情况,该参数指定曲线应延伸超出极端数据点的距离。 但这仅影响绘制曲线的位置; 密度估计仍然会在不存在数据的范围内平滑,导致它在分布的极端处人为地低:
我使用反复试验来获得正确的 cut 值,即12
。 有关更多详细信息,请参阅下面的代码。
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
x = np.random.normal(0, 3, 100)
y = np.random.normal(0, 1, 100)
fig, ax = plt.subplots()
ax.scatter(x,y, marker='.')
ax.set_aspect('equal')
ax.set(xlim=(-13,13))
ax.set(ylim=(-8,8))
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
sns.kdeplot(x,y, fill=True, thresh=0, alpha=0.7, cmap='coolwarm', ax=ax, cbar=True, cbar_ax = cax, cut=12) # `shade_lowest` is now deprecated in favor of `thresh`
colour = ax.collections[1].get_facecolor()
Output 图片:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.