Seaborn Factorplot generates extra empty plots below actual plot

前端 未结 3 978
有刺的猬
有刺的猬 2020-12-09 10:10

All,

I am trying to plot two Factorplots using subplots function and Seaborn library. I was able to plot two plots separately using below code. However,

3条回答
  •  误落风尘
    2020-12-09 10:54

    I came here because I had exactly the same problem with empty plots below the grid of sns.catplot plots. @jojo's answer is excellent and explains everything in detail. As a newbie to these things I cannot improve that, except for one point. The quick solution, that is closing the figures that sns.catplot() created, can be most easily implemented by simply calling plt.close(plt.gcf()) directly after sns.catplot(). That way, you don't have to think about numerical indexes of figures to close.

    At least the following works for me and creates exactly what I needed. 14 different catplots in a nice 5x3 arrangement. No empty plots, not below the grid and not at the end of it.

    cols = [ "Year Built", "Year Remod/Add", "Bsmt Full Bath", "Bsmt Half Bath",
        "Full Bath", "Half Bath", "Bedroom AbvGr", "Kitchen AbvGr", "TotRms AbvGrd",
        "Fireplaces", "Garage Yr Blt", "Garage Cars", "Mo Sold", "Yr Sold" ] # len 14
    fig, axs = plt.subplots(5, 3, figsize = (18, 24))
    for i, ax in enumerate(axs.flatten()):
        if i < len(cols):
            sns.catplot(x = cols[i], y = "SalePrice", data = df, ax = ax, kind = "boxen")
            plt.close(plt.gcf()) # close current figure just created by catplot
        else:
            ax.set_axis_off() # hide empty subplot at end of grid
    

提交回复
热议问题