10000 Simplify GridSpec setup in make_axes_gridspec. · matplotlib/matplotlib@cc77c0b · GitHub
[go: up one dir, main page]

Skip to content

Commit cc77c0b

Browse files
committed
Simplify GridSpec setup in make_axes_gridspec.
Currently, make_axes_gridspec uses two nested gridspecs, first a horizontal (1, 2) gridspec and second a vertical (3, 1) gridspec to position an axes A and the associated left-colorbar C as ``` A. AC A. ``` (and similarly for colorbars on other sides of the main axes). Instead, this can be done with a single (3, 2) gridspec to position both A and C.
1 parent 05173e3 commit cc77c0b

File tree

1 file changed

+20
-35
lines changed

1 file changed

+20
-35
lines changed

lib/matplotlib/colorbar.py

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,45 +1518,30 @@ def make_axes_gridspec(parent, *, location=None, orientation=None,
15181518
wh_space = 2 * pad / (1 - pad)
15191519

15201520
if location in ('left', 'right'):
1521-
# for shrinking
1522-
height_ratios = [
1523-
(1-anchor[1])*(1-shrink), shrink, anchor[1]*(1-shrink)]
1524-
1521+
gs = parent.get_subplotspec().subgridspec(
1522+
3, 2, wspace=wh_space, hspace=0,
1523+
height_ratios=[(1-anchor[1])*(1-shrink), shrink, anchor[1]*(1-shrink)])
15251524
if location == 'left':
1526-
gs = parent.get_subplotspec().subgridspec(
1527-
1, 2, wspace=wh_space,
1528-
width_ratios=[fraction, 1-fraction-pad])
1529-
ss_main = gs[1]
1530-
ss_cb = gs[0].subgridspec(
1531-
3, 1, hspace=0, height_ratios=height_ratios)[1]
1525+
gs.set_width_ratios([fraction, 1 - fraction - pad])
1526+
ss_main = gs[:, 1]
1527+
ss_cb = gs[1, 0]
15321528
else:
1533-
gs = parent.get_subplotspec().subgridspec(
1534-
1, 2, wspace=wh_space,
1535-
width_ratios=[1-fraction-pad, fraction])
1536-
ss_main = gs[0]
1537-
ss_cb = gs[1].subgridspec(
1538-
3, 1, hspace=0, height_ratios=height_ratios)[1]
1529+
gs.set_width_ratios([1 - fraction - pad, fraction])
1530+
ss_main = gs[:, 0]
1531+
ss_cb = gs[1, 1]
15391532
else:
1540-
# for shrinking
1541-
width_ratios = [
1542-
anchor[0]*(1-shrink), shrink, (1-anchor[0])*(1-shrink)]
1543-
1544-
if location == 'bottom':
1545-
gs = parent.get_subplotspec().subgridspec(
1546-
2, 1, hspace=wh_space,
1547-
height_ratios=[1-fraction-pad, fraction])
1548-
ss_main = gs[0]
1549-
ss_cb = gs[1].subgridspec(
1550-
1, 3, wspace=0, width_ratios=width_ratios)[1]
1551-
aspect = 1 / aspect
1533+
gs = parent.get_subplotspec().subgridspec(
1534+
2, 3, hspace=wh_space, wspace=0,
1535+
width_ratios=[anchor[0]*(1-shrink), shrink, (1-anchor[0])*(1-shrink)])
1536+
if location == 'top':
1537+
gs.set_height_ratios([fraction, 1 - fraction - pad])
1538+
ss_main = gs[1, :]
1539+
ss_cb = gs[0, 1]
15521540
else:
1553-
gs = parent.get_subplotspec().subgridspec(
1554-
2, 1, hspace=wh_space,
1555-
height_ratios=[fraction, 1-fraction-pad])
1556-
ss_main = gs[1]
1557-
ss_cb = gs[0].subgridspec(
1558-
1, 3, wspace=0, width_ratios=width_ratios)[1]
1559-
aspect = 1 / aspect
1541+
gs.set_height_ratios([1 - fraction - pad, fraction])
1542+
ss_main = gs[0, :]
1543+
ss_cb = gs[1, 1]
1544+
aspect = 1 / aspect
15601545

15611546
parent.set_subplotspec(ss_main)
15621547
if panchor is not False:

0 commit comments

Comments
 (0)
0