153
153
*ax*
154
154
None | parent axes object(s) from which space for a new
155
155
colorbar axes will be stolen. If a list of axes is given
156
- they will be resized to make room for the colorbar axes.
156
+ they will all be resized to make room for the colorbar axes.
157
157
*use_gridspec*
158
158
False | If *cax* is None, a new *cax* is created as an instance of
159
159
Axes. If *ax* is an instance of Subplot and *use_gridspec* is True,
@@ -258,7 +258,7 @@ def __init__(self, ax, cmap=None,
258
258
values = None ,
259
259
boundaries = None ,
260
260
orientation = 'vertical' ,
261
- location = 'right ' ,
261
+ ticklocation = 'auto ' ,
262
262
extend = 'neither' ,
263
263
spacing = 'uniform' , # uniform or proportional
264
264
ticks = None ,
@@ -282,7 +282,6 @@ def __init__(self, ax, cmap=None,
282
282
self ._inside = self ._slice_dict [extend ]
283
283
self .spacing = spacing
284
284
self .orientation = orientation
285
- self .location = location
286
285
self .drawedges = drawedges
287
286
self .filled = filled
288
287
self .extendfrac = extendfrac
@@ -292,7 +291,12 @@ def __init__(self, ax, cmap=None,
292
291
self .outline = None
293
292
self .patch = None
294
293
self .dividers = None
295
- self .set_label ('' )
294
+
295
+ if ticklocation == 'auto' :
296
+ ticklocation = 'bottom' if orientation == 'horizontal' else 'right'
297
+ self .ticklocation = ticklocation
298
+
299
+ self .set_label (label )
296
300
if cbook .iterable (ticks ):
297
301
self .locator = ticker .FixedLocator (ticks , nbins = len (ticks ))
298
302
else :
@@ -342,14 +346,14 @@ def config_axis(self):
342
346
if self .orientation == 'vertical' :
343
347
ax .xaxis .set_ticks ([])
344
348
# location is either one of 'bottom' or 'top'
345
- ax .yaxis .set_label_position (self .location )
346
- ax .yaxis .set_ticks_position (self .location )
349
+ ax .yaxis .set_label_position (self .ticklocation )
350
+ ax .yaxis .set_ticks_position (self .ticklocation )
347
351
else :
348
352
ax .yaxis .set_ticks ([])
349
353
# location is either one of 'left' or 'right'
350
- ax .xaxis .set_label_position (self .location )
354
+ ax .xaxis .set_label_position (self .ticklocation )
351
355
# XXX This wasn't enabled before...
352
- ax .xaxis .set_ticks_position (self .location )
356
+ ax .xaxis .set_ticks_position (self .ticklocation )
353
357
354
358
self ._set_label ()
355
359
@@ -956,112 +960,91 @@ def update_bruteforce(self, mappable):
956
960
957
961
958
962
@docstring .Substitution (make_axes_kw_doc )
959
- def make_axes_orig (parent , ** kw ):
960
- '''
961
- Resize and reposition a parent axes, and return a child
962
- axes suitable for a colorbar::
963
-
964
- cax, kw = make_axes(parent, **kw)
965
-
966
- Keyword arguments may include the following (with defaults):
967
-
968
- *orientation*
969
- 'vertical' or 'horizontal'
970
-
971
- %s
972
-
973
- All but the first of these are stripped from the input kw set.
974
-
975
- Returns (cax, kw), the child axes and the reduced kw dictionary.
976
- '''
977
- orientation = kw .setdefault ('orientation' , 'vertical' )
978
- fraction = kw .pop ('fraction' , 0.15 )
979
- shrink = kw .pop ('shrink' , 1.0 )
980
- aspect = kw .pop ('aspect' , 20 )
981
- #pb = transforms.PBox(parent.get_position())
982
- pb = parent .get_position (original = True ).frozen ()
983
- if orientation == 'vertical' :
984
- pad = kw .pop ('pad' , 0.05 )
985
- x1 = 1.0 - fraction
986
- pb1 , pbx , pbcb = pb .splitx (x1 - pad , x1 )
987
- pbcb = pbcb .shrunk (1.0 , shrink ).anchored ('C' , pbcb )
988
- anchor = kw .pop ('anchor' , (0.0 , 0.5 ))
989
- panchor = kw .pop ('panchor' , (1.0 , 0.5 ))
990
- else :
991
- pad = kw .pop ('pad' , 0.15 )
992
- pbcb , pbx , pb1 = pb .splity (fraction , fraction + pad )
993
- pbcb = pbcb .shrunk (shrink , 1.0 ).anchored ('C' , pbcb )
994
- aspect = 1.0 / aspect
995
- anchor = kw .pop ('anchor' , (0.5 , 1.0 ))
996
- panchor = kw .pop ('panchor' , (0.5 , 0.0 ))
997
- parent .set_position (pb1 )
998
- parent .set_anchor (panchor )
999
- fig = parent .get_figure ()
1000
- cax = fig .add_axes (pbcb )
1001
- cax .set_aspect (aspect , anchor = anchor , adjustable = 'box' )
1002
- return cax , kw
1003
-
1004
-
1005
- @docstring .Substitution (make_axes_kw_doc )
1006
- def make_axes (parent , location = None , orientation = None , fraction = 0.15 , shrink = 1.0 , aspect = 20 , ** kw ):
963
+ def make_axes (parents , location = None , orientation = None , fraction = 0.15 , shrink = 1.0 , aspect = 20 , ** kw ):
1007
964
locations = ["left" , "right" , "top" , "bottom" ]
1008
965
if orientation is not None and location is not None :
1009
966
raise TypeError ('position and orientation are mutually exclusive. Consider ' \
1010
- 'setting the position to any of %s' % ',' .join (locations ))
1011
-
1012
- # must pump out an orientation for colorbar creation
1013
- if location in ['left' , 'right' ]:
1014
- kw ['orientation' ] = 'vertical'
1015
- kw ['location' ] = location
1016
- anchor = kw .pop ('anchor' , (0.0 , 0.5 ))
1017
- # define the parent's anchor to be next to the new colorbar axes
1018
- panchor = kw .pop ('panchor' , (1.0 , 0.5 ))
1019
- else :
1020
- kw ['orientation' ] = 'horizontal'
1021
- kw ['location' ] = location
1022
- anchor = kw .pop ('anchor' , (0.5 , 1.0 ))
1023
- # define the parent's anchor to be next to the new colorbar axes
1024
- panchor = kw .pop ('panchor' , (0.5 , 0.0 ))
1025
-
1026
- # define padding between colorbar axes and parent axes in axes coordinates.
1027
- # For best outcomes, pad is best at 0.15 when location is "bottom"
1028
- if location == 'bottom' :
1029
- pad = kw .pop ('pad' , 0.0 )
1030
- else :
1031
- pad = kw .pop ('pad' , 0.00 )
1032
-
1033
- if isinstance (parent , list ):
1034
- parents_bbox = mtrans .Bbox .union ([ax .get_position (original = True ).frozen () \
1035
- for ax in parent ])
967
+ 'setting the position to any of %s' % ', ' .join (locations ))
968
+
969
+ # allow the user to not specify the location by specifying the orientation instead
970
+ if location is None :
971
+ location = 'right' if orientation == 'vertical' else 'bottom'
972
+
973
+ if location not in locations :
974
+ raise ValueError ('Invalid colorbar location. Must be one of %s' % ', ' .join (locations ))
975
+
976
+ default_location_settings = {'left' : {'anchor' : (1.0 , 0.5 ),
977
+ 'panchor' : (0.0 , 0.5 ),
978
+ 'pad' : 0.10 ,
979
+ 'orientation' : 'vertical' },
980
+ 'right' : {'anchor' : (0.0 , 0.5 ),
981
+ 'panchor' : (1.0 , 0.5 ),
982
+ 'pad' : 0.05 ,
983
+ 'orientation' : 'vertical' },
984
+ 'top' : {'anchor' : (0.5 , 0.0 ),
985
+ 'panchor' : (0.5 , 1.0 ),
986
+ 'pad' : 0.05 ,
987
+ 'orientation' : 'horizontal' },
988
+ 'bottom' : {'anchor' : (0.5 , 1.0 ),
989
+ 'panchor' : (0.5 , 0.0 ),
990
+ 'pad' : 0.15 , # backwards compat
991
+ 'orientation' : 'horizontal' },
992
+ }
993
+
994
+ loc_settings = default_location_settings [location ]
995
+
996
+ # put appropriate values into the kw dict for passing back to
997
+ # the Colorbar class
998
+ kw ['orientation' ] = loc_settings ['orientation' ]
999
+ kw ['ticklocation' ] = location
1000
+
1001
+ anchor = kw .pop ('anchor' , loc_settings ['anchor' ])
1002
+ parent_anchor = kw .pop ('panchor' , loc_settings ['panchor' ])
1003
+ pad = kw .pop ('pad' , loc_settings ['pad' ])
1004
+
1005
+
1006
+ # turn parents into a list if it is not already
1007
+ if not isinstance (parents , (list , tuple )):
1008
+ parents = [parents ]
1009
+
1010
+ fig = parents [0 ].get_figure ()
1011
+ if not all (fig is ax .get_figure () for ax in parents ):
1012
+ raise ValueError ('Unable to create a colorbar axes as not all ' + \
1013
+ 'parents share the same figure.' )
1014
+
1015
+ # take a bounding box around all of the given axes
1016
+ parents_bbox = mtrans .Bbox .union ([ax .get_position (original = True ).frozen () \
1017
+ for ax in parents ])
1036
1018
1037
1019
pb = parents_bbox
1038
1020
if location in ('left' , 'right' ):
1039
1021
if location == 'left' :
1040
- pbcb , _ , pb1 = pb .splitx (1 - fraction , fraction + pad )
1022
+ pbcb , _ , pb1 = pb .splitx (fraction , fraction + pad )
1041
1023
else :
1042
1024
pb1 , _ , pbcb = pb .splitx (1 - fraction - pad , 1 - fraction )
1043
- pbcb = pbcb .shrunk (1.0 , shrink ).anchored ('C' , pbcb )
1044
-
1025
+ pbcb = pbcb .shrunk (1.0 , shrink ).anchored (anchor , pbcb )
1045
1026
else :
1046
- if location == 'top' :
1047
- pb1 , _ , pbcb = pb .splity (1 - fraction - pad , fraction )
1048
- else :
1027
+ if location == 'bottom' :
1049
1028
pbcb , _ , pb1 = pb .splity (fraction , fraction + pad )
1050
- pbcb = pbcb .shrunk (shrink , 1.0 ).anchored ('C' , pbcb )
1029
+ else :
1030
+ pb1 , _ , pbcb = pb .splity (1 - fraction - pad , 1 - fraction )
1031
+ pbcb = pbcb .shrunk (shrink , 1.0 ).anchored (anchor , pbcb )
1032
+
1051
1033
# define the aspect ratio in terms of y's per x rather than x's per y
1052
1034
aspect = 1.0 / aspect
1053
1035
1036
+ # define a transform which takes us from old axes coordinates to
1037
+ # new axes coordinates
1054
1038
shrinking_trans = mtrans .BboxTransform (parents_bbox , pb1 )
1055
1039
1056
- for ax in parent :
1040
+ # transform each of the axes in parents using the new transform
1041
+ for ax in parents :
1057
1042
new_posn = shrinking_trans .transform (ax .get_position ())
1058
1043
new_posn = mtrans .Bbox (new_posn )
1059
1044
ax .set_position (new_posn )
1060
- if panchor is not False :
1061
- ax .set_anchor (panchor )
1045
+ if parent_anchor is not False :
1046
+ ax .set_anchor (parent_anchor )
1062
1047
1063
- # XXX test all axes must be on the same figure...
1064
- fig = parent [0 ].get_figure ()
1065
1048
cax = fig .add_axes (pbcb )
1066
1049
cax .set_aspect (aspect , anchor = anchor , adjustable = 'box' )
1067
1050
return cax , kw
@@ -1101,6 +1084,8 @@ def make_axes_gridspec(parent, **kw):
1101
1084
'''
1102
1085
1103
1086
orientation = kw .setdefault ('orientation' , 'vertical' )
1087
+ kw ['ticklocation' ] = 'auto'
1088
+
1104
1089
fraction = kw .pop ('fraction' , 0.15 )
1105
1090
shrink = kw .pop ('shrink' , 1.0 )
1106
1091
aspect = kw .pop ('aspect' , 20 )
0 commit comments