-1

I am not able to understand why my plots are being plotted outside my subplots.

Can anyone tell me where I am going wrong?

Here is my code snippet:

figure, (ax1,ax2,ax3) = plt.subplots(1, 3, sharex=True)
figure.suptitle('repeat_retailer')

sns.catplot(ax= ax1, data=ds, x="repeat_retailer", y="distance_from_home", hue="fraud", jitter = True)

sns.catplot(ax= ax2, data=ds, x="repeat_retailer", y="distance_from_last_transaction", hue="fraud", jitter = True)

sns.catplot(ax= ax3, data=ds, x="repeat_retailer", y="ratio_to_median_purchase_price", hue="fraud", jitter = True)

plt.show()

The output is as shown in the image.

output

tdy
  • 36,675
  • 19
  • 86
  • 83
  • [`catplot`](https://seaborn.pydata.org/generated/seaborn.catplot.html) is a [figure-level plot](https://seaborn.pydata.org/faq#what-do-figure-level-and-axes-level-mean), so it can't be placed into axes. In fact it doesn't have an `ax` parameter at all, and you should have gotten `UserWarning: catplot is a figure-level function and does not accept target axes` – tdy Feb 04 '23 at 19:43
  • See also https://seaborn.pydata.org/faq#why-isn-t-seaborn-drawing-the-plot-where-i-tell-it-to – mwaskom Feb 05 '23 at 13:37

1 Answers1

-3

It seems that there is a problem with the size of the subplots or the figure size. You can try specifying the figure size using figure.set_size_inches(width, height), and also adjusting the size of the subplots using fig.tight_layout() or specifying the height and width of the subplots using fig.subplots_adjust(hspace=height, wspace=width).

Additionally, you can also check if there is any overlap with the title and subplots by specifying the subplot_adjust parameter.

A general syntax of subplot is as follows: Syntax: fig, ax = plt.subplots(nrows, ncols)

  • nrows: The number of rows of subplots in the figure.
  • ncols: The number of columns of subplots in the figure.

For example, in the code

    figure, (ax1,ax2,ax3) = plt.subplots(1, 3, sharex=True), 

there is one row and three columns of subplots in the figure, and ax1, ax2, and ax3 are the axes objects for each subplot.

You can then plot on each subplot using the corresponding axis object, e.g.

    sns.catplot(ax= ax1, data=ds, x="repeat_retailer", 
    y="distance_from_home", hue="fraud", jitter = True) 

plots on the first subplot using ax1.

EDITED

Then maybe the issue is with seaborns catplots. Have you tried using scatter plot and see if this works?

    figure, (ax1, ax2, ax3) = plt.subplots(1, 3, sharex=True)

    sns.scatterplot(ax=ax1, data=ds, x="repeat_retailer",                 
    y="distance_from_home", hue="fraud", jitter=True)
    sns.scatterplot(ax=ax2, data=ds, x="repeat_retailer", y="distance_from_last_transaction", hue="fraud", jitter=True)
    sns.scatterplot(ax=ax3, data=ds, x="repeat_retailer", y="ratio_to_median_purchase_price", hue="fraud", jitter=True)

    plt.show()