def colorbar(self,mappable=None,location='right',size="5%",pad='2%',fig=None,ax=None,**kwargs):
"""
Add colorbar to axes associated with a map.
The colorbar axes instance is created using the axes_grid toolkit.
.. tabularcolumns:: |l|L|
============== ====================================================
Keywords Description
============== ====================================================
mappable the Image, ContourSet, etc. to which the colorbar
applies. Default None, matplotlib.pyplot.gci() is
used to retrieve the current image mappable.
location where to put colorbar ('top','bottom','left','right')
Default 'right'.
size width of colorbar axes (string 'N%', where N is
an integer describing the fractional width of the parent
axes). Default '5%'.
pad Padding between parent axes and colorbar axes in
same units as size. Default '2%'.
fig Figure instance the map axes instance is associated
with. Default None, and matplotlib.pyplot.gcf() is used
to retrieve the current active figure instance.
ax The axes instance which the colorbar will be
associated with. Default None, searches for self.ax,
and if None uses matplotlib.pyplot.gca().
\**kwargs extra keyword arguments passed on to
colorbar method of the figure instance.
============== ====================================================
Returns a matplotlib colorbar instance.
"""
# get current axes instance (if none specified).
ax = ax or self._check_ax()
# get current figure instance (if none specified).
if fig is None or mappable is None:
import matplotlib.pyplot as plt
if fig is None:
fig = plt.gcf()
# get current mappable if none specified.
if mappable is None:
mappable = plt.gci()
# create colorbar axes uses axes_grid toolkit.
divider = make_axes_locatable(ax)
if location in ['left','right']:
orientation = 'vertical'
elif location in ['top','bottom']:
orientation = 'horizontal'
else:
raise ValueError('location must be top,bottom,left or right')
cax = divider.append_axes(location, size=size, pad=pad)
# create colorbar.
cb = fig.colorbar(mappable,orientation=orientation,cax=cax,**kwargs)
fig.sca(ax) # reset parent axes as current axes.
return cb
评论列表
文章目录