1

My seaborn plot is working fine. I am having issues in legend. The legend is placed inside the plot bydefault. But when I place it outside, the title goes missing.

enter image description here

My code: import seaborn as sns

sns.barplot(x='xdata',y='ydata',hue='Rank',
            data=df,
            palette='jet_r')
plt.gcf().autofmt_xdate()
plt.legend(bbox_to_anchor=(1,0.9),loc='upper left')
plt.show()

Present output: the legend is placed outside but the title is missing. How to solve this? enter image description here

Mainland
  • 4,110
  • 3
  • 25
  • 56
  • 3
    Seaborn 0.11.2 has a new function `sns.move_legend(ax, bbox_to_anchor=(1,0.9),loc='upper left')` that by default keeps the legend intact. Note that you need `ax = sns.barplot(....)` to grab the `ax`. You might need to call `plt.tight_layout()` just before `plt.show()` to fit everything nicely into the plot. – JohanC Jan 31 '22 at 19:29
  • @JohanC Yes! it solved the issue. Thanks for letting me know about `sns.move_legend()` which I did not know before. – Mainland Jan 31 '22 at 23:12

0 Answers0