複数パネルのカラーバーのレイアウト

Matplotlibで複数のパネルがある場合にカラーバーをレイアウトする方法について記載する。

問題意識としては、カラーバーが図に対して妙に長くなってしまったり、1つのパネルにのみカラーバーをつけた場合に、そのパネルの図のサイズが小さくなり、大きさが不揃いになってしまったり、といったところ。

a=np.random.rand(144).reshape(12,12)
b=np.random.rand(144).reshape(12,12)
c=np.random.rand(144).reshape(12,12)

fig=plt.figure(figsize(9,3),dpi=200)
ax0=plt.subplot(131)
ax1=plt.subplot(132)
ax2=plt.subplot(133)
im0=ax0.imshow(a)
im1=ax1.imshow(b)
im2=ax2.imshow(c,cmap="RdBu")

plt.tight_layout(w_pad=1)

cb1=fig.colorbar(im1,orientation="vertical",\
                 ax=[ax0,ax1],shrink=0.5,extend="both",\
                 fraction=0.1,pad=0.01)
cb1.set_ticks([0,0.5,1])
cb1.set_ticklabels(["0","0.5","1"])
cb1.ax.tick_params(labelsize=8)
cb1.set_label(label="units",fontsize=8,labelpad=0.5)

cb2=fig.colorbar(im2,orientation="vertical",\
                 ax=[ax2],shrink=0.5,extend="both",\
                 fraction=0.1,pad=0.02)
cb2.set_ticks([0,0.5,1])
cb2.set_ticklabels(["0","0.5","1"])
cb2.ax.tick_params(labelsize=8)
cb2.set_label(label="mg/m2/day",fontsize=8,labelpad=0.5)

plt.savefig("Fig1.png",dpi=200,bbox_inches="tight",pad_inches=0.1)
  • colorbarの各パラメータの説明
    • shrinkはカラーバーのサイズの倍数
    • fractionはカラーバーのために空けるスペース
    • padは図とカラーバーの間のスペース
  • tight_layoutはカラーバーの命令の前に実施

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA