0

I made a scatterplot where I put the legend just outside (beneath) the plot. When saving my plot, the legend is cut off halfway.

What is the best method to fix this?

scalebar = ScaleBar(1, location='lower right') 

plt.style.use('seaborn')
plt.scatter(x['xcoord'], x['ycoord'], c='lightgrey', s=25)
plt.scatter(x[x['var1']==1]['xcoord'], x[x['var1']==1]['ycoord'], c='dimgrey', s=35)
plt.scatter(x[x['var2']==1]['xcoord'], x[x['var2']==1]['ycoord'], c='red', s=180, marker="+")
plt.gca().legend(('dwelling', 'var1', 'var2'), frameon= True, facecolor='white', loc='lower center',
        bbox_to_anchor=(0, -0.22, 1, 0), fontsize=12,) #this places the legend outside the plot.
plt.gca().add_artist(scalebar)
plt.tick_params(axis='x', colors='white')
plt.tick_params(axis='y', colors='white')
plt.savefig('test.pdf')
plt.show()
TvCasteren
  • 449
  • 3
  • 18

1 Answers1

1

You can try setting bbox_inches to tight in plt.savefig()

plt.savefig('test.pdf', bbox_inches='tight')
Jason Yu
  • 101
  • 3