matplotlib - 带有 Seaborn 联合网格图的多个边缘图

标签 matplotlib plot seaborn boxplot jointplot

我想绘制一个带有多个边缘图的联合网格图,如下所示:

enter image description here

引用代码为:

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

penguins = sns.load_dataset("penguins")
print(penguins['species'])

plt.figure(figsize=(12,10))
g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species")

g.plot_joint(sns.scatterplot)
g.plot_marginals(sns.boxplot)

plt.show()

最佳答案

如果您想要在边缘轴上绘制stripplot图,您只需添加hue参数即可:

g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species")
g.plot_joint(sns.scatterplot)
g.plot_marginals(sns.stripplot, hue="species", dodge=True)

但是 boxplot 当前无法处理仅分配一个坐标变量的 hue,因此您需要单独绘制每个边缘图:

g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species")
g.plot_joint(sns.scatterplot)
sns.boxplot(penguins, x=g.hue, y=g.y, ax=g.ax_marg_y)
sns.boxplot(penguins, y=g.hue, x=g.x, ax=g.ax_marg_x)

enter image description here

关于matplotlib - 带有 Seaborn 联合网格图的多个边缘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73216548/

相关文章:

python - 在 matplotlib 中操纵线宽以进行孵化

python - Matplotlib 轴标签

python - 使用 DataFrame.plot 制作带子图的图表 -- 如何使用 ax 参数

r - ggplot sec_axis 我可以使用向量作为反式公式吗?

python-3.x - sns.pairplot 显示密度曲线而不是直方图

python - 如何在 scikit 中使用外部数据来执行 PCA/LDA?

python - 支持向量回归 (SVR) 在 Ubuntu 18.04 LTS 中未绘制任何图形

r - 如何在R中绘制树(和松鼠)?

python - Seaborn 热图裁剪 y 轴刻度标签

python - seaborn彩条移动标签