60

I use a trick to draw a colorbar whose height matches the master axes. The code is like

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)

This trick works good. However, since a new axis is appended, the current instance of the figure becomes cax - the appended axis. As a result, if one performs operations like

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

the text will be drawn on cax instead of ax - the axis to which im belongs.

Meanwhile, gcf().axes shows both axes.

My question is: How to make the current axis instance (returned by gca()) the original axis to which im belongs.

cottontail
  • 10,268
  • 18
  • 50
  • 51
Liang
  • 1,015
  • 1
  • 10
  • 13

2 Answers2

111

Use plt.sca(ax) to set the current axes, where ax is the Axes object you'd like to become active.

Aaron
  • 10,133
  • 1
  • 24
  • 40
Joe Kington
  • 275,208
  • 71
  • 604
  • 463
0

Instead of changing the current Axes instance, it's possible to simply index the desired Axes instance the list of Axes in the Figure (as OP mentioned):

plt.gcf().axes[0].text(0, 0, 'whatever')

or in the specific example, since the subplot is already assigned to a variable name, simply use it as is.

ax.text(0, 0, 'whatever')
cottontail
  • 10,268
  • 18
  • 50
  • 51