Skip to content

Commit 0d7e4f3

Browse files
author
Jeff Whitaker
committed
add colorbar method.
1 parent 3bcf756 commit 0d7e4f3

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

lib/mpl_toolkits/basemap/__init__.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from matplotlib.lines import Line2D
2929
from matplotlib.transforms import Bbox
3030
from mpl_toolkits.basemap import pyproj
31+
from mpl_toolkits.axes_grid1 import make_axes_locatable
3132
import sys, os, math
3233
from .proj import Proj
3334
import numpy as np
@@ -3670,6 +3671,61 @@ def drawmapscale(self,lon,lat,lon0,lat0,length,barstyle='simple',\
36703671
rets = [ret.set_zorder(zorder) for ret in rets]
36713672
return rets
36723673

3674+
def colorbar(self,mappable=None,location='right',size="5%",pad=0.1,fig=None,ax=None,**kwargs):
3675+
"""
3676+
Add colorbar to axes associated with a map.
3677+
The colorbar axes instance is created using the axes_grid toolkit.
3678+
3679+
.. tabularcolumns:: |l|L|
3680+
3681+
Keywords Description
3682+
============== ====================================================
3683+
mappable the Image, ContourSet, etc. to which the colorbar
3684+
applies. Default None, matplotlib.pyplot.gci() is
3685+
used to retrieve the current image mappable.
3686+
location where to put colorbar ('top','bottom','left','right')
3687+
Default 'right'.
3688+
size width of colorbar axes (string 'N%', where N is
3689+
an integer describing the percentage of the parent
3690+
axes). Default '5%'.
3691+
pad Padding between parent axes and colorbar axes in
3692+
inches. Default 0.1.
3693+
fig Figure instance the map axes instance is associated
3694+
with. Default None, and matplotlib.pyplot.gcf() is used
3695+
to retrieve the current active figure instance.
3696+
ax The axes instance which the colorbar will be
3697+
associated with. Default None, searches for self.ax,
3698+
and if None uses matplotlib.pyplot.gca().
3699+
\**kwargs extra keyword arguments passed on to
3700+
colorbar method of the figure instance.
3701+
============== ====================================================
3702+
3703+
Returns a matplotlib colorbar instance.
3704+
"""
3705+
# get current axes instance (if none specified).
3706+
ax = ax or self._check_ax()
3707+
# get current figure instance (if none specified).
3708+
if fig is None or mappable is None:
3709+
import matplotlib.pyplot as plt
3710+
if fig is None:
3711+
fig = plt.gcf()
3712+
# get current mappable if none specified.
3713+
if mappable is None:
3714+
mappable = plt.gci()
3715+
# create colorbar axes uses axes_grid toolkit.
3716+
divider = make_axes_locatable(ax)
3717+
if location in ['left','right']:
3718+
orientation = 'vertical'
3719+
elif location in ['top','bottom']:
3720+
orientation = 'horizontal'
3721+
else:
3722+
raise ValueError('location must be top,bottom,left or right')
3723+
cax = divider.append_axes(location, size=size, pad=pad)
3724+
# create colorbar.
3725+
cb = fig.colorbar(mappable,orientation=orientation,cax=cax,**kwargs)
3726+
fig.sca(ax) # reset parent axes as current axes.
3727+
return cb
3728+
36733729
def nightshade(self,date,color="k",delta=0.25,alpha=0.5,ax=None,zorder=2):
36743730
"""
36753731
Shade the regions of the map that are in darkness at the time

0 commit comments

Comments
 (0)