0

I am plotting a relplot with Seaborn, but getting the legend (and an empty axis plot) printed under the main plot.

Here is how it looks like (in 2 photos, as my screen isn't that big): main plot legend printed under it :-/

Here is the code I used:

fig, axes = plt.subplots(1, 1, figsize=(12, 5))

clean_df['tax_class_at_sale'] = clean_df['tax_class_at_sale'].apply(str)

sns.relplot(x="sale_price_millions", y='gross_sqft_thousands', hue="neighborhood", data=clean_df, ax=axes)

fig.suptitle('Sale Price by Neighborhood', position=(.5,1.05), fontsize=20)
fig.tight_layout()
fig.show()

Does someone has an idea how to fix that, so that the legend (maybe much smaller, but it's not a problem) is printed next to the plot, and the empty axis disappears?

Here is my dataset form (in 2 screenshot, to capture all columns. "sale_price_millions" is the target column)

first part of columns second part of columns

ZelelB
  • 1,836
  • 7
  • 45
  • 71
  • The legend and the additional figure are present because you use a workaround for the fact that relplot produces its own figure. However if you only want to have one figure, why create a different one? Why not use a `scatterplot`? – ImportanceOfBeingErnest Jun 02 '19 at 17:59
  • Does this answer your question? [Move seaborn plot legend to a different position](https://stackoverflow.com/questions/27019079/move-seaborn-plot-legend-to-a-different-position) – Trenton McKinney Sep 12 '21 at 21:04

1 Answers1

0

Since you failed to provide a Minimal, Complete, and Verifiable example, no one can give you a final working answer because we can't reproduce your figure. Nevertheless, you can try specifying the location for placing the legend as following and see if it works as you want

sns.relplot(x="sale_price_millions", y='gross_sqft_thousands', hue="neighborhood", data=clean_df, ax=axes)
plt.legend(loc=(1.05, 0.5))
Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • Sorry but my dataset is relatively big, so I've just made a screenshot (in 2 photos, as I couldn't have all columns in just one) and edit the question. Btw, you're proposed code didn't work. It still plots the legend under the main big plot, and the empty axis as well. On top of that, now with the code you proposed, it print another legend above the already printed legend, but a big smaller. In the edit you can find the form of my df. – ZelelB Jun 02 '19 at 18:13