2024.6.20 複数のimshow+カラーバー【matplotlib】
code:plot1.py
import numpy as np
import matplotlib.pyplot as plt
w = np.random.rand(5, 5)
x = np.random.rand(5, 5)
y = np.random.rand(5, 5)*2
z = np.random.rand(5, 5)/2
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax1.set_title('w')
ax1.grid()
img1 = ax1.imshow(w)
fig.colorbar(img1)
ax2 = fig.add_subplot(2,2,2)
ax2.set_title('x')
ax2.grid()
img2 = ax2.imshow(x)
fig.colorbar(img2)
ax3 = fig.add_subplot(2,2,3)
ax3.set_title('y')
ax3.grid()
img3 = ax3.imshow(y)
fig.colorbar(img3)
ax4 = fig.add_subplot(2,2,4)
ax4.set_title('z')
ax4.grid()
img4 = ax4.imshow(z)
fig.colorbar(img4)
plt.subplots_adjust(wspace=0.2, hspace=0.4) # 余白設定
plt.savefig('fig.svg')
plt.show()
結果:
https://scrapbox.io/files/667381ad4fd77c001df4086f.svg
グラフごとに独立したカラーバーが描かれる。