图例在保存图形时被切断。

huangapple go评论63阅读模式
英文:

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')

huangapple
  • 本文由 发表于 2023年2月23日 23:47:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定