5

enter image description here

I made the plot above using seaborn but I am not able to place the legend outside plot properly. Please note that the legend is cut off on the right side of the image. This is how it looks in real, I did not cut it manually. This is the code I am using:

sns.lineplot(x="Time", y='Anomaly', style='country', hue='region', size='area', sizes=(1., 4), data=df)
# Put the legend out of the figure
plt.subplots_adjust(right=0.2)
plt.legend(bbox_to_anchor=(.95, 1), loc=2, borderaxespad=0.)
plt.tight_layout()
plt.show()

-- EDIT:

here is a data to replicate this issue: https://www.dropbox.com/s/w4gd447e22zb5yk/subset.csv?dl=0

user308827
  • 21,227
  • 87
  • 254
  • 417
  • For a detailed guide on how to place the legend out of the plot, see [this answer](https://stackoverflow.com/questions/4700614/how-to-put-the-legend-out-of-the-plot/43439132#43439132). Here you place the legend at 95% of the axes size. However for it to be outside the axes, you need to use 100% or more. Hence `bbox_to_anchor=(1, 1)` or even `(1.02, 1)` might be a good idea. – ImportanceOfBeingErnest Nov 03 '18 at 09:06
  • thanks @ImportanceOfBeingErnest, when I do 1.05 the legend is no longer even visible – user308827 Nov 03 '18 at 14:33
  • That's why I marked this as duplicate. In particular, the "Postprocessing" section of [this answer](https://stackoverflow.com/questions/4700614/how-to-put-the-legend-out-of-the-plot/43439132#43439132) explains possible solutions. – ImportanceOfBeingErnest Nov 03 '18 at 15:09
  • @ImportanceOfBeingErnest, even after doing post-processing e.g. subplots_adjust, the legend is partly invisible – user308827 Nov 03 '18 at 16:22
  • So if you used e.g. `subplots_adjust(right=0.8)` and the legend is still partially hidden, you will want to try `right=0.7` next and see what happens until you're satisfied. If you find yourself in need of values lower than 0.5, you may consider using shorter labels in the legend or increasing the figure width. – ImportanceOfBeingErnest Nov 03 '18 at 16:37
  • I tried `subplots_adjust(right=0.3)` and still same results. Not sure why that is happening – user308827 Nov 03 '18 at 17:10
  • If you see no visual difference between using `right=0.3` and `right=0.8` you are not operating on the figure you show. Of course you can provide a [mcve], such that one has a change of helping here. – ImportanceOfBeingErnest Nov 03 '18 at 17:13
  • @ImportanceOfBeingErnest, thanks for the feedback, I have added data and updated the code to show what I am doing – user308827 Nov 03 '18 at 17:30
  • It does not make sense to call `tight_layout` after setting the subplot parameters; it will simply overwrite the previously set parameters. – ImportanceOfBeingErnest Nov 03 '18 at 17:34

1 Answers1

6

You have not specified a sample set for us to be able to test the implementation and generate the plot, but with a toy initialization, modifying bbox_to_anchor seems to do the trick. See matplotlib's legend guide.

bbox_to_anchor controls manual legend placement. Setting it to (1,1) places it in the top right corner.

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

Sample plot with modification: enter image description here

haxtar
  • 1,962
  • 3
  • 20
  • 44