英文:
Legend outside of plot is cut off when saving figure
问题
我制作了一个散点图,将图例放在图表外部(下方)。在保存图表时,图例被截断了一半。
最佳解决方法是什么?
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) # 这将图例放在图表外部。
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()
英文:
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()
答案1
得分: 1
你可以尝试在 plt.savefig()
中将 bbox_inches
设置为 tight
。
plt.savefig('test.pdf', bbox_inches='tight')
英文:
You can try setting bbox_inches
to tight
in plt.savefig()
plt.savefig('test.pdf', bbox_inches='tight')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论