pythonmatplotlibseabornswarmplot

How to set a seaborn swarmplot size and change legend location?


I'm trying to plot a swarmplot with seaborn but I'm struggling with the legend of the plot, I managed to change its position if the figure, but now it is cropped and no information is shown:

enter image description here

I moved the legend with:

plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

I changed the size of the figure with no luck

plt.figure(figsize=(12, 10))

I also tried tight_layout() but it just removes the legend from the plot.

Any ideas on how to make it appear on the side of the plot nicely without any cropping on the side or bottom?

Thank you


Solution

  • The legend is not taken into account when plt.tight_layout() tries to optimize the spaces. However you can set the spaces manually using plt.subplots_adjust()

    The relevant argument here is the right side of the figure, so plt.subplots_adjust(right=0.7) would be a good start. 0.7 means that the right part of the axes is at 70% of the total figure size, which should give you enough space for the legend.
    Then, you may want to change that number according to your needs.