如何在一个窗口上绘制多个FacetGrid?

时间:2020-08-02 21:10:22

标签: python matplotlib seaborn

在下面的代码中,我可以通过向每个窗口传递ax=ax[i]参数来在一个窗口中放置两个简单的海洋图,不适用于FacetGrid()here也曾问过类似的问题,想知道是否有人对此有一个想法。谢谢!

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

df = sns.load_dataset('tips')

########################
# Works :)
########################
fig,ax = plt.subplots(nrows=2)
sns.regplot(x='total_bill', y='tip', data=df, ax=ax[0])         # plot #1
sns.boxplot(x='day', y='total_bill', data=df, ax=ax[1])         # plot #2
plt.show()

########################
# Does not work :(
########################
fig,ax = plt.subplots(nrows=2)
g = sns.FacetGrid(df, col="time", ax=ax[0])                             # FacetGrid #1
g.map(plt.hist, "tip")

g = sns.FacetGrid(df, col="sex", hue="smoker", col_wrap=2, ax=ax[1])    # FacetGird #2
g.map(plt.scatter, "total_bill", "tip", alpha=.7)
g.axes[-1].legend() 
plt.show()

1 个答案:

答案 0 :(得分:0)

这是因为FacetGrid本身会生成子图并创建一个图形。 FacetGrid不允许使用子图。唯一的解决方案是将FacetGrid合并在一起以创建一个图-但是,除非您的轴相同,否则这并非易事。

可能的解决方法如下: combine different seaborn facet grids into single plot