0

I've built a violinplot using seaborn and I want to save it as an image. The figure is printed in the console like this:

enter image description here

But it gets saved with a cropped legend:

enter image description here

Does anyone know how could I fix that? The code:

import seaborn as sns 
import matplotlib.pyplot as plt 

sns.set('paper', color_codes=True) 
protein_name = input('What is the name of your protein?') 
my_pal = {"3mer": "red", "9mer": "blue"} fig = 

sns.violinplot(y= 'Percentage', x= 'Secondary structure',hue= 'Type of library', data = df, split = True, palette = my_pal) 
fig.set_title('Distribution of secondary structures in' + protein_name) 
leg=plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.5) 
leg.set_title('Type of library')
plt.ylim(0, 100)

plt.savefig(protein_name + "_violin_plots.jpg") 
Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51
AdeEla
  • 279
  • 1
  • 3
  • 13
  • You need to see this differently. The legend is simply outside of the figure all the time. To expand the figure when saving, you may add `plt.savefig(protein_name + "_violin_plots.jpg", bbox_inches="tight")` – ImportanceOfBeingErnest Jul 24 '18 at 15:32
  • That worked. Thank you! – AdeEla Jul 24 '18 at 15:37
  • I will mark as duplicate of the question on how to put the legend out the plot, since at least one answer there explains well why this happens and shows some possible solutions. (`bbox_inches="tight"` is just the easiest here) – ImportanceOfBeingErnest Jul 24 '18 at 15:41

0 Answers0