43
43
import matplotlib .colors as colors
44
44
import matplotlib .contour as contour
45
45
import matplotlib .cm as cm
46
- import matplotlib .gridspec as gridspec
47
46
import matplotlib .patches as mpatches
48
47
import matplotlib .path as mpath
49
48
import matplotlib .ticker as ticker
55
54
_log = logging .getLogger (__name__ )
56
55
57
56
_make_axes_param_doc = """
57
+ location : None or {'left', 'right', 'top', 'bottom'}
58
+ The location, relative to the parent axes, where the colorbar axes
59
+ is created. It also determines the *orientation* of the colorbar
60
+ (colorbars on the left and right are vertical, colorbars at the top
61
+ and bottom are horizontal). If None, the location will come from the
62
+ *orientation* if it is set (vertical colorbars on the right, horizontal
63
+ ones at the bottom), or default to 'right' if *orientation* is unset.
64
+ orientation : None or {'vertical', 'horizontal'}
65
+ The orientation of the colorbar. It is preferrable to set the
66
+ *location* of the colorbar, as that also determines the *orientation*;
67
+ passing incompatible values for *location* and *orientation* raises an
68
+ exception.
58
69
fraction : float, default: 0.15
59
70
Fraction of original axes to use for colorbar.
60
71
shrink : float, default: 1.0
@@ -1363,6 +1374,27 @@ def remove(self):
1363
1374
ax .set_subplotspec (subplotspec )
1364
1375
1365
1376
1377
+ def _normalize_location_orientation (location , orientation ):
1378
+ if location is None :
1379
+ location = cbook ._check_getitem (
1380
+ {None : "right" , "vertical" : "right" , "horizontal" : "bottom" },
1381
+ orientation = orientation )
1382
+ loc_settings = cbook ._check_getitem ({
1383
+ "left" : {"location" : "left" , "orientation" : "vertical" ,
1384
+ "anchor" : (1.0 , 0.5 ), "panchor" : (0.0 , 0.5 ), "pad" : 0.10 },
1385
+ "right" : {"location" : "right" , "orientation" : "vertical" ,
1386
+ "anchor" : (0.0 , 0.5 ), "panchor" : (1.0 , 0.5 ), "pad" : 0.05 },
1387
+ "top" : {"location" : "top" , "orientation" : "horizontal" ,
1388
+ "anchor" : (0.5 , 0.0 ), "panchor" : (0.5 , 1.0 ), "pad" : 0.05 },
1389
+ "bottom" : {"location" : "bottom" , "orientation" : "horizontal" ,
1390
+ "anchor" : (0.5 , 1.0 ), "panchor" : (0.5 , 0.0 ), "pad" : 0.15 },
1391
+ }, location = location )
1392
+ if orientation is not None and orientation != loc_settings ["orientation" ]:
1393
+ # Allow the user to pass both if they are consistent.
1394
+ raise TypeError ("location and orientation are mutually exclusive" )
1395
+ return loc_settings
1396
+
1397
+
1366
1398
@docstring .Substitution (_make_axes_param_doc ,
1E79
_make_axes_other_param_doc )
1367
1399
def make_axes (parents , location = None , orientation = None , fraction = 0.15 ,
1368
1400
shrink = 1.0 , aspect = 20 , ** kw ):
@@ -1376,16 +1408,6 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
1376
1408
----------
1377
1409
parents : `~.axes.Axes` or list of `~.axes.Axes`
1378
1410
The Axes to use as parents for placing the colorbar.
1379
-
1380
- location : None or {'left', 'right', 'top', 'bottom'}
1381
- The position, relative to *parents*, where the colorbar axes
1382
- should be created. If None, the value will either come from the
1383
- given ``orientation``, else it will default to 'right'.
1384
-
1385
- orientation : None or {'vertical', 'horizontal'}
1386
- The orientation of the colorbar. Typically, this keyword shouldn't
1387
- be used, as it can be derived from the ``location`` keyword.
1388
-
1389
1411
%s
1390
1412
1391
1413
Returns
@@ -1400,47 +1422,11 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
1400
1422
----------------
1401
1423
%s
1402
1424
"""
1403
- locations = ["left" , "right" , "top" , "bottom" ]
1404
- if orientation is not None and location is not None :
1405
- raise TypeError ('position and orientation are mutually exclusive. '
1406
- 'Consider setting the position to any of {}'
1407
- .format (', ' .join (locations )))
1408
-
1409
- # provide a default location
1410
- if location is None and orientation is None :
1411
- location = 'right'
1412
-
1413
- # allow the user to not specify the location by specifying the
1414
- # orientation instead
1415
- if location is None :
1416
- location = 'right' if orientation == 'vertical' else 'bottom'
1417
-
1418
- cbook ._check_in_list (locations , location = location )
1419
-
1420
- default_location_settings = {'left' : {'anchor' : (1.0 , 0.5 ),
1421
- 'panchor' : (0.0 , 0.5 ),
1422
- 'pad' : 0.10 ,
1423
- 'orientation' : 'vertical' },
1424
- 'right' : {'anchor' : (0.0 , 0.5 ),
1425
- 'panchor' : (1.0 , 0.5 ),
1426
- 'pad' : 0.05 ,
1427
- 'orientation' : 'vertical' },
1428
- 'top' : {'anchor' : (0.5 , 0.0 ),
1429
- 'panchor' : (0.5 , 1.0 ),
1430
- 'pad' : 0.05 ,
1431
- 'orientation' : 'horizontal' },
1432
- 'bottom' : {'anchor' : (0.5 , 1.0 ),
1433
- 'panchor' : (0.5 , 0.0 ),
1434
- 'pad' : 0.15 , # backwards compat
1435
- 'orientation' : 'horizontal' },
1436
- }
1437
-
1438
- loc_settings = default_location_settings [location ]
1439
-
1425
+ loc_settings = _normalize_location_orientation (location , orientation )
1440
1426
# put appropriate values into the kw dict for passing back to
1441
1427
# the Colorbar class
1442
1428
kw ['orientation' ] = loc_settings ['orientation' ]
1443
- kw ['ticklocation' ] = location
1429
+ location = kw ['ticklocation' ] = loc_settings [ ' location' ]
1444
1430
1445
1431
anchor = kw .pop ('anchor' , loc_settings ['anchor' ])
1446
1432
parent_anchor = kw .pop ('panchor' , loc_settings ['panchor' ])
@@ -1533,7 +1519,8 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
1533
1519
1534
1520
1535
1521
@docstring .Substitution (_make_axes_param_doc , _make_axes_other_param_doc )
1536
- def make_axes_gridspec (parent , * , fraction = 0.15 , shrink = 1.0 , aspect = 20 , ** kw ):
1522
+ def make_axes_gridspec (parent , * , location = None , orientation = None ,
1523
+ fraction = 0.15 , shrink = 1.0 , aspect = 20 , ** kw ):
1537
1524
"""
1538
1525
Create a `~.SubplotBase` suitable for a colorbar.
1539
1526
@@ -1542,9 +1529,6 @@ def make_axes_gridspec(parent, *, fraction=0.15, shrink=1.0, aspect=20, **kw):
1542
1529
1543
1530
This function is similar to `.make_axes`. Primary differences are
1544
1531
1545
- - `.make_axes_gridspec` only handles the *orientation* keyword
1546
- and cannot handle the *location* keyword.
1547
-
1548
1532
- `.make_axes_gridspec` should only be used with a `.SubplotBase` parent.
1549
1533
1550
1534
- `.make_axes` creates an `~.axes.Axes`; `.make_axes_gridspec` creates a
@@ -1560,7 +1544,6 @@ def make_axes_gridspec(parent, *, fraction=0.15, shrink=1.0, aspect=20, **kw):
1560
1544
----------
1561
1545
parent : `~.axes.Axes`
1562
1546
The Axes to use as parent for placing the colorbar.
1563
-
1564
1547
%s
1565
1548
1566
1549
Returns
@@ -1573,63 +1556,54 @@ def make_axes_gridspec(parent, *, fraction=0.15, shrink=1.0, aspect=20, **kw):
1573
1556
1574
1557
Other Parameters
1575
1558
----------------
1576
- orientation : {'vertical', 'horizontal'}, default: 'vertical'
1577
- The orientation of the colorbar.
1578
-
1579
1559
%s
1580
1560
"""
1581
1561
1582
- orientation = kw .setdefault ('orientation' , 'vertical' )
1583
- kw ['ticklocation' ] = 'auto'
1562
+ loc_settings = _normalize_location_orientation (location , orientation )
1563
+ kw ['orientation' ] = loc_settings ['orientation' ]
1564
+ location = kw ['ticklocation' ] = loc_settings ['location' ]
1584
1565
1585
- x1 = 1 - fraction
1566
+ pad = loc_settings ["pad" ]
1567
+ wh_space = 2 * pad / (1 - pad )
1586
1568
1587
1569
# for shrinking
1588
1570
pad_s = (1 - shrink ) * 0.5
1589
1571
wh_ratios = [pad_s , shrink , pad_s ]
1590
1572
1591
- # we need to none the tree of layoutboxes because
1592
- # constrained_layout can't remove and replace the tree
1593
- # hierarchy w/o a seg fault.
1594
- gs = parent .get_subplotspec ().get_gridspec ()
1595
- layoutbox .nonetree (gs ._layoutbox )
1596
- gs_from_subplotspec = gridspec .GridSpecFromSubplotSpec
1597
- if orientation == 'vertical' :
1598
- pad = kw .pop ('pad' , 0.05 )
1599
- wh_space = 2 * pad / (1 - pad )
1600
- gs = gs_from_subplotspec (1 , 2 ,
1601
- subplot_spec = parent .get_subplotspec (),
1602
- wspace = wh_space ,
1603
- width_ratios = [x1 - pad , fraction ])
1604
- gs2 = gs_from_subplotspec (3 , 1 ,
1605
- subplot_spec = gs [1 ],
1606
- hspace = 0. ,
1607
- height_ratios = wh_ratios )
1608
- anchor = (0.0 , 0.5 )
1609
- panchor = (1.0 , 0.5 )
1610
- else :
1611
- pad = kw .pop ('pad' , 0.15 )
1612
- wh_space = 2 * pad / (1 - pad )
1613
- gs = gs_from_subplotspec (2 , 1 ,
1614
- subplot_spec = parent .get_subplotspec (),
1615
- hspace = wh_space ,
1616
- height_ratios = [x1 - pad , fraction ])
1617
- gs2 = gs_from_subplotspec (1 , 3 ,
1618
- subplot_spec = gs [1 ],
1619
- wspace = 0. ,
1620
- width_ratios = wh_ratios )
1573
+ # we need to none the tree of layoutboxes because constrained_layout can't
1574
+ # remove and replace the tree hierarchy w/o a segfault.
1575
+ layoutbox .nonetree (parent .get_subplotspec ().get_gridspec ()._layoutbox )
1576
+ if location == "left" :
1577
+ gs = parent .get_subplotspec ().subgridspec (
1578
+ 1 , 2 , wspace = wh_space , width_ratios = [fraction , 1 - fraction - pad ])
1579
+ ss_main = gs [1 ]
1580
+ ss_cb = gs [0 ].subgridspec (3 , 1 , hspace = 0 , height_ratios = wh_ratios )[1 ]
1581
+ elif location == "right" :
1582
+ gs = parent .get_subplotspec ().subgridspec (
1583
+ 1 , 2 , wspace = wh_space , width_ratios = [1 - fraction - pad , fraction ])
1584
+ ss_main = gs [0 ]
1585
+ ss_cb = gs [1 ].subgridspec (3 , 1 , hspace = 0 , height_ratios = wh_ratios )[1 ]
1586
+ elif location == "top" :
1587
+ gs = parent .get_subplotspec ().subgridspec (
1588
+ 2 , 1 , hspace = wh_space , height_ratios = [fraction , 1 - fraction - pad ])
1589
+ ss_main = gs [1 ]
1590
+ ss_cb = gs [0 ].subgridspec (1 , 3 , wspace = 0 , width_ratios = wh_ratios )[1 ]
1591
+ aspect = 1 / aspect
1592
+ else : # "bottom"
1593
+ gs = parent .get_subplotspec ().subgridspec (
1594
+ 2 , 1 , hspace = wh_space , height_ratios = [1 - fraction - pad , fraction ])
1595
+ ss_main = gs [0 ]
1596
+ ss_cb = gs [1 ].subgridspec (1 , 3 , wspace = 0 , width_ratios = wh_ratios )[1 ]
1621
1597
aspect = 1 / aspect
1622
- anchor = (0.5 , 1.0 )
1623
- panchor = (0.5 , 0.0 )
1624
1598
1625
- parent .set_subplotspec (gs [ 0 ] )
1599
+ parent .set_subplotspec (ss_main )
1626
1600
parent .update_params ()
1627
1601
parent ._set_position (parent .figbox )
1628
- parent .set_anchor (panchor )
1602
+ parent .set_anchor (loc_settings [ " panchor" ] )
1629
1603
1630
1604
fig = parent .get_figure ()
1631
- cax = fig .add_subplot (gs2 [ 1 ] , label = "<colorbar>" )
1632
- cax .set_aspect (aspect , anchor = anchor , adjustable = 'box' )
1605
+ cax = fig .add_subplot (ss_cb , label = "<colorbar>" )
1606
+ cax .set_aspect (aspect , anchor = loc_settings [ " anchor" ] , adjustable = 'box' )
1633
1607
return cax , kw
1634
1608
1635
1609
0 commit comments