我使用一个技巧画出高度与主轴匹配的色条。代码如下:
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)
这个技巧很有效。然而,由于添加了一个新的轴,当前图像实例就变成了cax - 即添加的轴。因此,如果执行诸如
plt.text(0,0,'whatever')
文本将在cax上绘制,而不是im所属的轴ax。
与此同时,gcf().axes显示两个轴。
我的问题是:如何使当前轴实例(由gca()返回)成为im所属的原始轴。