首页 > 解决方案 > 减少 Seaborn KDE 绘图边距

问题描述

嗨,我有下面的代码,可以在联合图的边缘生成多个 KDE 图

h = sns.JointGrid('SECS', 'EPOCH', df)

for name, group in df.groupby("QUERY"):
    sns.kdeplot(group["SECS"], ax=h.ax_marg_x, legend=False)
    sns.kdeplot(group["EPOCH"], ax=h.ax_marg_y, vertical=True, legend=False)
    h.ax_joint.plot(group["SECS"], group["EPOCH"], ".", ms=5)

在此处输入图像描述

我很想知道如何限制边距,以便图表从 0 开始,而不是从 -20000 开始,那里有很多死区。

标签: pythonmatplotlibchartsseaborn

解决方案


您可以像首先绘制数据一样访问 a JointGridusing的主图。ax_joint如果您打印类型,h.ax_joint您可以看到它是matplotlib.axes._subplots.AxesSubplot. 因此,您可以像通常使用 matplotlib 一样操纵轴的限制。

例如:

h = sns.JointGrid('SECS', 'EPOCH', df)

for name, group in df.groupby("QUERY"):
    sns.kdeplot(group["SECS"], ax=h.ax_marg_x, legend=False)
    sns.kdeplot(group["EPOCH"], ax=h.ax_marg_y, vertical=True, legend=False)
    h.ax_joint.plot(group["SECS"], group["EPOCH"], ".", ms=5)

h.ax_joint.set_xlim(0,90000)

推荐阅读