使用tight_layout()

时间:2016-04-29 18:26:53

标签: python matplotlib

我使用matplotlib.gridspectight_layout()来创建复杂的绘图布局。我当前的代码看起来像

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib
from matplotlib.ticker import MaxNLocator

fig = plt.figure(figsize=(15,15))
gs1 = gridspec.GridSpec(8, 2)

gs1.update(left=0.05, right=0.95, wspace=0.05, hspace=0.05)

ax1 = plt.subplot(gs1[0:4, 0]) # Original
ax2 = plt.subplot(gs1[0:4, 1]) # Model
ax3 = plt.subplot(gs1[4:8, 0]) # Residual+Sky
ax4 = plt.subplot(gs1[4:7, 1]) # SB profile
ax5 = plt.subplot(gs1[7:8, 1])# SB residuals

# Hide tick labels
plt.setp(ax1.get_yticklabels(), visible=False)
plt.setp(ax1.get_xticklabels(), visible=False)
plt.setp(ax2.get_yticklabels(), visible=False)
plt.setp(ax2.get_xticklabels(), visible=False)
plt.setp(ax3.get_yticklabels(), visible=False)
plt.setp(ax3.get_xticklabels(), visible=False)
plt.setp(ax4.get_xticklabels(), visible=False)

ax4.invert_yaxis()
ax4.set_ylabel(r'Surface Brightness, $\mu$ [mag arcsec$^{-2}$]')
ax5.set_ylabel(r'$\Delta\mu$')
ax5.set_xlabel('Semi-major Axis [arcsec]')
ax5.grid(b=True)
ax4.set_xscale('log')
ax5.set_xscale('log')

gs1.tight_layout(fig)

nbins = len(ax5.get_xticklabels())
ax5.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax4.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))

# Show the plot
plt.show()

生成一个看起来像

的布局

enter image description here

我需要做的是

  1. 缩小ax4和ax5(两个右下角的子图)之间的垂直空间,或

  2. 使ax4和ax5共享相同的x轴,使得子图之间的空间为零

  3. 我非常喜欢gridspectight_layout()格式化图表的方式,但我不知道如何使用#34; force"各个子图之间的间距。有没有一种简单的方法可以同时使用matplotlib.gridspectight_layout()

1 个答案:

答案 0 :(得分:2)

您可以使用轴实例的get_position和set_position方法更改图中轴的位置(http://matplotlib.org/api/axes_api.html)。

get_position返回一个Bbox实例,您可以使用get_points获取[[x0,y0],[x1,y1]]形式的2x2 numpy数组,其中x0,y0,x1,y1是你的数字坐标轴。

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib
from matplotlib.ticker import MaxNLocator

fig = plt.figure(figsize=(15,15))
gs1 = gridspec.GridSpec(8, 2)

gs1.update(left=0.05, right=0.95, wspace=0.05, hspace=0.05)

ax1 = plt.subplot(gs1[0:4, 0]) # Original
ax2 = plt.subplot(gs1[0:4, 1]) # Model
ax3 = plt.subplot(gs1[4:8, 0]) # Residual+Sky
ax4 = plt.subplot(gs1[4:7, 1]) # SB profile
ax5 = plt.subplot(gs1[7:8, 1])# SB residuals

# Hide tick labels
plt.setp(ax1.get_yticklabels(), visible=False)
plt.setp(ax1.get_xticklabels(), visible=False)
plt.setp(ax2.get_yticklabels(), visible=False)
plt.setp(ax2.get_xticklabels(), visible=False)
plt.setp(ax3.get_yticklabels(), visible=False)
plt.setp(ax3.get_xticklabels(), visible=False)
plt.setp(ax4.get_xticklabels(), visible=False)

ax4.invert_yaxis()
ax4.set_ylabel(r'Surface Brightness, $\mu$ [mag arcsec$^{-2}$]')
ax5.set_ylabel(r'$\Delta\mu$')
ax5.set_xlabel('Semi-major Axis [arcsec]')
ax5.grid(b=True)
ax4.set_xscale('log')
ax5.set_xscale('log')

gs1.tight_layout(fig)

nbins = len(ax5.get_xticklabels())
ax5.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax4.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))

# change axis location of ax5
pos4 = ax4.get_position()
pos5 = ax5.get_position()

points4 = pos4.get_points()
points5 = pos5.get_points()

points5[1][1]=points4[0][1]

pos5.set_points(points5)

ax5.set_position(pos5)
# Show the plot
plt.show()

此代码应该产生: plot with adjusted ax5