matplotlib:具有相同长宽比的子图的水平间距

时间:2018-08-26 19:57:07

标签: python matplotlib plot

我试图用三列pcolormesh图绘制两行,并为所有图组合一个颜色条。到目前为止,它似乎有效。但是,我确定我没有使用最优雅的方式... 我唯一的问题是,我无法再减小水平间距。以下行应将水平间距设置为零:

fig.subplots_adjust(left=0.05, right=0.98, top=0.93, bottom=0.00, wspace=0, hspace=0.03)

但这不能与

结合使用
ax.set_aspect('equal')

我附上了一个小代码段,该代码段创建了下图: Example figure

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

rows = 2
columns = 3

fig = plt.figure()
gs = gridspec.GridSpec(rows+1, columns)

lines = []

x = np.linspace(1,10,100)
y = x
X, Y = np.meshgrid(x,y)
Z = np.random.rand(100,100)

lines = []
for i in range(rows):
    lines.append([])
    for j in range(columns):
        ax = fig.add_subplot(gs[i, j])

        line = ax.pcolormesh(X, Y, Z, cmap=plt.cm.Reds)
        lines[i].append(line)

        ax.set_aspect('equal')

        for tick in ax.get_xticklabels():
            tick.set_rotation(45)

        if i!=rows-1:
            ax.set_xticklabels([])
        if j!=0:
            ax.set_yticklabels([])
        #title
        props = dict(boxstyle='round', facecolor='white', alpha=0.7)
        ax.text(0.05, 0.95, "plot (%i, %i)" % (i,j), transform=ax.transAxes, fontsize=5,
                verticalalignment='top', bbox=props)
        ax.tick_params(labelsize=7)

cb_ax = fig.add_subplot(gs[-1,:])
cb_ax.set_aspect(0.05)

cbar = fig.colorbar(lines[0][0], cax=cb_ax, orientation='horizontal')
cb_ax.tick_params(labelsize=7)

fig.subplots_adjust(left=0.05, right=0.98, top=0.93, bottom=0.00, wspace=0, hspace=0.03)
#fig.tight_layout()
fig.text(0.5, 0.2, "x axis", ha='center', va='center')
fig.text(0.5, 0.97, "overall title", ha='center', va='center')
fig.text(0.02, 0.5, "y axis", ha='center', va='center', rotation='vertical')
fig.text(0.5, 0.02, "quantity [unit]", ha='center', va='center',)
plt.savefig("test.png", dpi=600)

0 个答案:

没有答案