Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib: change the current axis instance (i.e., gca())

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.

like image 951
Liang Avatar asked Oct 28 '13 00:10

Liang


People also ask

What is GCA () in matplotlib?

The gca() function in pyplot module of matplotlib library is used to get the current Axes instance on the current figure matching the given keyword args, or create one. Syntax: matplotlib.pyplot.gca(\*\*kwargs)

What is fig GCA ()?

gca() method. The gca() method figure module of matplotlib library is used to get the current axes.

What is GCA and GCF in matplotlib?

Difference Between Matplotlib gca and gcf:GCA. GCF. stands for get current axis. stands for get current figure. gives the reference of the current axes.


1 Answers

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

like image 114
Joe Kington Avatar answered Oct 13 '22 19:10

Joe Kington