0

I'm working with a lot of csv files each representing a different submarine channel (underwater river). I have been writing functions to create various plots with seaborn (violin plots, distplots etc.) for each channel and have encountered a problem when trying to loop over and plot a number of different columns in a dataframe as subplots. The problem is that the three subplots generated appear blank and the plots produced, while correct, appear individually, not as the three subplots I'd like. I used a very similar piece of code to generate 3 violin plots and it worked fine.

What am I missing? I am pretty new to python and can't figure it out.

def distPlots(df, fname):
    """
    df : dataframe
    filename : string for file export
    """
    copy = df.drop('Channel', axis=1)
    cols = copy.columns
    col_labels = ["Width (m)", "Height (m)", "Aspect ratio"]
    fig, ax = plt.subplots(1, 3, figsize=(17, 5))
    for var, label, subplot in zip(cols, col_labels, ax.flatten()):
        sns.displot(x=var, hue="Channel", data=df, kde=True, bins=20, ax=subplot)
        subplot.set_xlabel(label)
    plt.tight_layout()
    plt.show()
    plt.draw()
    fig.savefig(fname)  

distPlots(data_chc1, 'ChC1_distPlots.pdf')
  • 1
    [`displot`](https://seaborn.pydata.org/generated/seaborn.displot.html#seaborn.displot) is a figure-level interface and has no `ax` parameter. – BigBen Feb 16 '21 at 16:22
  • ^^^ You probably just want to use [`histplot`](https://seaborn.pydata.org/generated/seaborn.histplot.html#seaborn.histplot) here. – BigBen Feb 16 '21 at 16:35
  • this is almost the same question, and the answer works: https://stackoverflow.com/questions/53310228/how-can-i-make-seaborn-distribution-subplots-in-a-loop – a11 Feb 16 '21 at 16:53
  • 1
    @a11 [`distplot`](https://seaborn.pydata.org/generated/seaborn.distplot.html) is deprecated. – BigBen Feb 16 '21 at 16:58
  • 1
    per @BigBen, the answer here shows how to do this with `histplot` https://stackoverflow.com/questions/63895392/seaborn-displot-is-not-plotting-within-defined-subplots – a11 Feb 17 '21 at 22:37

0 Answers0