我想绘制一个带有多个边缘图的联合网格图,如下所示:
引用代码为:
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)
关于matplotlib - 带有 Seaborn 联合网格图的多个边缘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73216548/