49

高さがマスター軸と一致するカラーバーを描画するトリックを使用します。コードは次のようになります

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np

ax = plt.subplot(111)
im = ax.imshow(np.arange(100).reshape((10,10)))

# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)

plt.colorbar(im, cax=cax)

このトリックはうまくいきます。ただし、新しい軸が追加されるため、Figure の現在のインスタンスは cax (追加された軸) になります。その結果、次のような操作を実行すると、

plt.text(0,0,'whatever')

テキストは ax ではなく cax (im が属する軸) に描画されます。

一方、gcf().axes は両方の軸を示します。

私の質問は、現在の軸インスタンス (gca() によって返される) を im が属する元の軸にする方法です。

4

1 に答える 1