调整gridspec中的填充/图形边距

时间:2017-02-21 02:47:16

标签: python matplotlib

我的子图在图中有点奇怪。顶部,底部和左侧都有大量的房间,我不需要,但右边距离右轴标签有点太近了。请参阅示例。The example figure with grey background to see the padding

我生成的代码如下。请注意我使用了tight_layout以及subplots_adjust,但它们都没有用。

fig = plt.figure(figsize=(7.5,10.))
fig.patch.set_facecolor('silver')
gs = gridspec.GridSpec(3,2,hspace=0.4,wspace=0.1)

ax1 = plt.subplot(gs[:,0])
a1 =  plt.plot(x, y, linestyle='-',color='b')
a1 = plt.ylabel('Y')
a1 = plt.ylim(zmax[c],zmin[c])
a1 = plt.xlabel('X')
a1 = plt.xticks([np.around(min(x),2),np.around(min(x)/2,2),0,np.around(max(x)/2,2),np.around(max(x),2)])
a1 = plt.title('(a)',fontsize=12)  

ax2 = plt.subplot(gs[0,1])
a2 = plt.plot(x2,y2, linestyle='',marker='o',color='b' )
a2 = plt.ylabel('value')
ax2.yaxis.tick_right()
ax2.yaxis.set_ticks_position('both')
ax2.yaxis.set_label_position("right")
a2 = plt.xlabel('number')
a2 = plt.title('(b)',fontsize=12) 

ax3 = plt.subplot(gs[1,1])
a3 = plt.plot(x3,y3, linestyle='--',color='b')
a3 = plt.ylabel('another y')
ax3.yaxis.tick_right()
ax3.yaxis.set_ticks_position('both') 
ax3.yaxis.set_label_position("right")
a3 = plt.xlabel('x')
a3 = plt.title('(c)',fontsize=12) 

ax4 = plt.subplot(gs[2,1])
a4 = plt.plot(x4,y4, linestyle='--',color='b')
a4 = plt.ylabel('y')
ax4.yaxis.tick_right()
ax4.yaxis.set_ticks_position('both')
ax4.yaxis.set_label_position("right")
a4 = plt.xlabel('x')
a4 = plt.title('(c)',fontsize=12) 

plt.suptitle('Title', fontsize=12,y=0.95)
plt.subplots_adjust(left=0, bottom=0, right=1, top=0, wspace=0, hspace=0)
#fig = plt.tight_layout()
plt.show()

问题是:如何在缩小其他边距并保留子图之间的填充的同时增加右边距?

3 个答案:

答案 0 :(得分:2)

您可以使用轴的get_position和set_position方法来处理此问题。

Munchkin.init('596-OEY-331')

修改

要设置整个无花果的位置,您必须设置子计划(http://matplotlib.org/api/figure_api.html#matplotlib.figure.SubplotParams

import matplotlib.pyplot as plt
ax = plt.subplot(111)
pos1 = ax.get_position() # get the original position 
pos2 = [pos1.x0 + 0.3, pos1.y0 + 0.3,  pos1.width / 2.0, pos1.height / 2.0] 
ax.set_position(pos2) # set a new position

答案 1 :(得分:2)

@ G.S的解决方案并没有奏效,但在仔细研究之前考虑的解决方案时激励着我。

plt.subplots_adjust(left=0.12, bottom=0.08, right=0.85, top=0.92, wspace=0.01, hspace=0.08) 实际上有效。关注您输入的值非常重要,尽管起初它并不是非常直观。如果left = value的值为right和top = bottom,则会抛出错误。它不是从边距到中心的距离,而是一个坐标。因此,未改变的数字左= 0且右= 1,底部= 0且顶部= 1。因此,移动边距基本上是将间隔上的值从0移动到1,并且需要一点点玩,直到看起来恰到好处。

答案 2 :(得分:2)

我认为您可以在GridSpec环境中执行此操作:

gs = gridspec.GridSpec(3,2,hspace=0.4,wspace=0.1)
gs.update(left=0.1,right=0.9,top=0.965,bottom=0.03,wspace=0.3,hspace=0.09)

您可以控制面板如何靠近左/右/上/下边距。对我来说效果很好。