简体   繁体   English

如何仅删除图表顶部的空间 - matplotlib

[英]How to remove space only at the top of the plot - matplotlib

I've seen many posts and answers online trying to answer this question. 我在网上看到很多帖子和答案试图回答这个问题。
However using bbox_inches = 'tight' the legend disappears. 但是,使用bbox_inches = 'tight' ,图例会消失。

This is one of my figures: 这是我的一个数字: 在此输入图像描述

Since I have the legend outside the plot frame, I would like to remove only the top and bottom white space. 由于我在绘图框外有图例,我想只删除顶部和底部的空白区域。

Anyone knows how to remove at least the top white space? 任何人都知道如何删除至少顶部的空白区域?

Thanks a lot! 非常感谢!

I usually don't use the bbox_inches = 'tight' feature, since it doesn't work very reliably, as you already found out. 我通常不使用bbox_inches = 'tight'功能,因为它不能非常可靠地工作,正如您已经发现的那样。 I'd rather produce a PDF with bounds and then crop them using external tools. 我宁愿生成带边界的PDF,然后使用外部工具裁剪它们。 To do this seamless from python, I use 为了从python无缝地做到这一点,我使用

os.system('pdfcrop %s %s &> /dev/null &'%(pdf_in, pdf_out))

Here, pdf_in is the PDF you produced from matplotlib and pdf_out will be your final result. 这里, pdf_in是您从matplotlib生成的PDF, pdf_out将是您的最终结果。

Have you tried using subplots_adjust() ? 你尝试过使用subplots_adjust()吗? See, for example, the answer of @DaveP to this question: Reduce left and right margins in matplotlib plot 例如,请参阅@DaveP对此问题的回答: 减少matplotlib图中的左右边距

Also, look at the answer by @Tian Chu to the same question. 另外,看看@Tian Chu对同一个问题的回答。

EDIT: This works for me: 编辑:这对我有用:

import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot([1,2,3],[5,6,7],'gs-')
plt.subplots_adjust(top=0.99, right=0.99)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM