次のコードを使用して、matplotlibを使用して簡単なヒートマップを作成しました。
fig,ax = plt.subplots(1)
# Display the image
ax.imshow(im)
a = np.array([[0.8, 2.4, 2.5, 3.9],
[2.4, 0.0, 4.0, 1.0],
[1.1, 2.4, 0.8, 4.3],
[0.6, 0.0, 0.3, 0.0],
[0.7, 1.7, 0.6, 2.6]])
ax1 = fig.add_subplot(111)
ax1.imshow(a, cmap='hot')
pos1 = ax1.get_position() # get the original position
pos2 = [0.13, 0.15, 0.3, 0.3]
ax1.set_position(pos2) # set a new position
このコードは機能しますが、唯一の問題は、ヒートマップを広くする方法がわからないことです。MPLでヒートマップの幅を設定するにはどうすればよいですか?
画像は、のextent=[x0, x1, y0, y1]
パラメータを使用して配置できますimshow
。を明示的に設定しないextent
と、xはからに-0.5
なりwidth-0.5
ます。これにより、目盛りがセルの中央の整数位置にうまく配置されます。
imshow
描かれた最後の画像にリセットXLIM、これらの必要性を明示的に設定されます。
オプションで、アスペクト比を「自動」に設定して、周囲の図の寸法に合わせて画像を引き伸ばすことができます。
from matplotlib import pyplot as plt
import numpy as np
fig, ax = plt.subplots(1)
# Display the image
im = np.random.randn(5, 10).cumsum(axis=0).cumsum(axis=1)
ax.imshow(im)
a = np.array([[0.8, 2.4, 2.5, 3.9],
[2.4, 0.0, 4.0, 1.0],
[1.1, 2.4, 0.8, 4.3],
[0.6, 0.0, 0.3, 0.0],
[0.7, 1.7, 0.6, 2.6]])
x0 = im.shape[1] - 0.5
x1 = x0 + a.shape[1]
ax.imshow(a, cmap='hot', extent=[x0, x1, -0.5, a.shape[0] - 0.5])
ax.set_xlim(-0.5, x1)
# ax.set_aspect('auto')
plt.show()
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加