【发布时间】:2018-06-02 04:12:21
【问题描述】:
我想尽量减少人物周围的空白,但不确定如何 a) 为我的图像周围的 savefig 命令精确指定一个边界框和 b) 为什么紧凑布局命令在我的工作示例中不起作用。
在我当前的示例中,我在对象/补丁周围紧密地设置了一个轴环境(如此紧密,以至于黄色对象和蓝色框几乎分别在左侧和底部被切断)。但是,这仍然给我左侧和底部的空白:
但是,在这种情况下,我不确定如何摆脱空白。 我认为可以按照Matplotlib tight_layout() doesn't take into account figure suptitle 的讨论指定边界框 但是插入
fig.tight_layout(rect=[0.1,0.1,0.9, 0.95]),
我知道如何通过插入一个填充整个图形等的轴对象来绕过这个,但这感觉就像一个愚蠢的黑客攻击。有没有简单快捷的方法来做到这一点?
我的代码是:
import matplotlib
from matplotlib import pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
from matplotlib.collections import PatchCollection
from matplotlib.patches import FancyBboxPatch
plt.ion()
fig, ax=plt.subplots(1)
ax.set_xlim([-0.38,7.6])
ax.set_ylim([-0.71,3.2])
ax.set_aspect(0.85)
#objects
circs2=[]
circs2.append( patches.Circle((-0.3, 1.225), 0.1,ec="none"))
circs2.append( patches.RegularPolygon ((-0.3,1.225+1.5),4, 0.1) )
coll2 = PatchCollection (circs2,zorder=10)
coll2.set_facecolor(['yellow', 'gold'])
ax.add_collection(coll2)
#squares
p_fancy=FancyBboxPatch((0.8,1.5),1.35,1.35,boxstyle="round,pad=0.1",fc='red', ec='k',alpha=0.7, zorder=1)
ax.add_patch(p_fancy)
x0=4.9
p_fancy=FancyBboxPatch((1.15+x0,-0.6),0.7*1.15,0.7*1.15,boxstyle="round,pad=0.1", fc='blue', ec='k',alpha=0.7, zorder=1)
ax.add_patch(p_fancy)
plt.axis('off')
fig.tight_layout(rect=[0.1,0.1,0.9, 0.95])
【问题讨论】:
标签: python matplotlib