From 9ac3c868979856c62bae0032893ed278b0b22a9a Mon Sep 17 00:00:00 2001 From: shawnchen1996 Date: Tue, 3 Mar 2020 19:30:43 +0800 Subject: [PATCH 01/16] update colorbar.py make_axes_gridspec --- lib/matplotlib/colorbar.py | 41 +++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index a4cd5719b3c2..8e9011e5a689 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1565,32 +1565,63 @@ def make_axes_gridspec(parent, *, location=None, orientation=None, location = kw['ticklocation'] = loc_settings['location'] pad = loc_settings["pad"] - wh_space = 2 * pad / (1 - pad) - - # for shrinking - pad_s = (1 - shrink) * 0.5 - wh_ratios = [pad_s, shrink, pad_s] # we need to none the tree of layoutboxes because constrained_layout can't # remove and replace the tree hierarchy w/o a segfault. layoutbox.nonetree(parent.get_subplotspec().get_gridspec()._layoutbox) if location == "left": + wh_space = 2 * pad / (1 - pad) + anchor = kw.pop('anchor', (0.0, 0.5)) + panchor = kw.pop('panchor', (1.0, 0.5)) + + # for shrinking + wh_ratios = [(1-anchor[1])*(1-shrink), + shrink, + anchor[1]*(1-shrink)] + gs = parent.get_subplotspec().subgridspec( 1, 2, wspace=wh_space, width_ratios=[fraction, 1-fraction-pad]) ss_main = gs[1] ss_cb = gs[0].subgridspec(3, 1, hspace=0, height_ratios=wh_ratios)[1] elif location == "right": + wh_space = 2 * pad / (1 - pad) + anchor = kw.pop('anchor', (0.0, 0.5)) + panchor = kw.pop('panchor', (1.0, 0.5)) + + # for shrinking + wh_ratios = [(1-anchor[1])*(1-shrink), + shrink, + anchor[1]*(1-shrink)] + gs = parent.get_subplotspec().subgridspec( 1, 2, wspace=wh_space, width_ratios=[1-fraction-pad, fraction]) ss_main = gs[0] ss_cb = gs[1].subgridspec(3, 1, hspace=0, height_ratios=wh_ratios)[1] elif location == "top": + wh_space = 2 * pad / (1 - pad) + anchor = kw.pop('anchor', (0.5, 1.0)) + panchor = kw.pop('panchor', (0.5, 0.0)) + + # for shrinking + wh_ratios = [(1-anchor[0])*(1-shrink), + shrink, + anchor[0]*(1-shrink)] + gs = parent.get_subplotspec().subgridspec( 2, 1, hspace=wh_space, height_ratios=[fraction, 1-fraction-pad]) ss_main = gs[1] ss_cb = gs[0].subgridspec(1, 3, wspace=0, width_ratios=wh_ratios)[1] aspect = 1 / aspect else: # "bottom" + wh_space = 2 * pad / (1 - pad) + anchor = kw.pop('anchor', (0.5, 1.0)) + panchor = kw.pop('panchor', (0.5, 0.0)) + + # for shrinking + wh_ratios = [(1-anchor[0])*(1-shrink), + shrink, + anchor[0]*(1-shrink)] + gs = parent.get_subplotspec().subgridspec( 2, 1, hspace=wh_space, height_ratios=[1-fraction-pad, fraction]) ss_main = gs[0] From 82486c5b006303e7edc424e273dc4dd7395f3c44 Mon Sep 17 00:00:00 2001 From: ShawnChen1996 Date: Wed, 4 Mar 2020 00:43:53 +0800 Subject: [PATCH 02/16] Update colorbar.py should fix the pep8 failure --- lib/matplotlib/colorbar.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 8e9011e5a689..d5a8c3ec7278 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1589,9 +1589,7 @@ def make_axes_gridspec(parent, *, location=None, orientation=None, panchor = kw.pop('panchor', (1.0, 0.5)) # for shrinking - wh_ratios = [(1-anchor[1])*(1-shrink), - shrink, - anchor[1]*(1-shrink)] + wh_ratios = [(1-anchor[1])*(1-shrink), shrink, anchor[1]*(1-shrink)] gs = parent.get_subplotspec().subgridspec( 1, 2, wspace=wh_space, width_ratios=[1-fraction-pad, fraction]) @@ -1603,9 +1601,7 @@ def make_axes_gridspec(parent, *, location=None, orientation=None, panchor = kw.pop('panchor', (0.5, 0.0)) # for shrinking - wh_ratios = [(1-anchor[0])*(1-shrink), - shrink, - anchor[0]*(1-shrink)] + wh_ratios = [(1-anchor[0])*(1-shrink), shrink, anchor[0]*(1-shrink)] gs = parent.get_subplotspec().subgridspec( 2, 1, hspace=wh_space, height_ratios=[fraction, 1-fraction-pad]) From a55b35c1a6330b8cd40c4305f555e9fa117438dd Mon Sep 17 00:00:00 2001 From: shawnchen1996 Date: Tue, 7 Apr 2020 15:36:33 +0800 Subject: [PATCH 03/16] fix `wh_ratios` orientation in `make_axes_gridspec` --- lib/matplotlib/colorbar.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index d5a8c3ec7278..896c9a3eb068 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1575,9 +1575,7 @@ def make_axes_gridspec(parent, *, location=None, orientation=None, panchor = kw.pop('panchor', (1.0, 0.5)) # for shrinking - wh_ratios = [(1-anchor[1])*(1-shrink), - shrink, - anchor[1]*(1-shrink)] + wh_ratios = [(1-anchor[1])*(1-shrink), shrink, anchor[1]*(1-shrink)] gs = parent.get_subplotspec().subgridspec( 1, 2, wspace=wh_space, width_ratios=[fraction, 1-fraction-pad]) @@ -1614,9 +1612,7 @@ def make_axes_gridspec(parent, *, location=None, orientation=None, panchor = kw.pop('panchor', (0.5, 0.0)) # for shrinking - wh_ratios = [(1-anchor[0])*(1-shrink), - shrink, - anchor[0]*(1-shrink)] + wh_ratios = [anchor[0]*(1-shrink), shrink, (1-anchor[0])*(1-shrink)] gs = parent.get_subplotspec().subgridspec( 2, 1, hspace=wh_space, height_ratios=[1-fraction-pad, fraction]) From 2c2a4ae9fcb7ca333fe41bf483a6ffd78196fd30 Mon Sep 17 00:00:00 2001 From: shawnchen1996 Date: Sat, 23 May 2020 21:43:23 +0800 Subject: [PATCH 04/16] add tests for cbar anchor --- lib/matplotlib/tests/test_colorbar.py | 48 +++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 8307fceb3384..5d5d649f9b50 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -622,3 +622,51 @@ def test_colorbar_int(clim): im = ax.imshow([[*map(np.int16, clim)]]) fig.colorbar(im) assert (im.norm.vmin, im.norm.vmax) == clim + + +def test_anchored_cbar_position_using_specgrid(): + data = np.arange(1200).reshape(30, 40) + levels = [0, 200, 400, 600, 800, 1000, 1200] + shrink = 0.5 + anchor_y = 0.3 + # vertival + fig, ax = plt.subplots() + cs = ax.contourf(data, levels=levels) + cbar = plt.colorbar( + cs, ax=ax, use_gridspec=True, + orientation='vertical', anchor=(1, anchor_y), shrink=shrink) + + # y1: the top of ax, y0: the bottom of ax, p0: the y postion of anchor + # cy1 : the top of colorbar ax, cy0: the bottom of colorbar ax + y1 = ax.get_position().y1 + y0 = ax.get_position().y0 + p0 = (y1 - y0) * anchor_y + y0 + cy1 = cbar.ax.get_position().y1 + cy0 = cbar.ax.get_position().y0 + + assert np.isclose( + [cy1, cy0], + [y1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + y0 * shrink] + ).all() + + # horizontal + shrink = 0.5 + anchor_x = 0.3 + fig, ax = plt.subplots() + cs = ax.contourf(data, levels=levels) + cbar = plt.colorbar( + cs, ax=ax, use_gridspec=True, + orientation='horizontal', anchor=(anchor_x, 1), shrink=shrink) + + # x1: the right of ax, x0: the left of ax, p0: the x postion of anchor + # cx1 : the right of colorbar ax, cx0: the left of colorbar ax + x1 = ax.get_position().x1 + x0 = ax.get_position().x0 + p0 = (x1 - x0) * anchor_x + x0 + cx1 = cbar.ax.get_position().x1 + cx0 = cbar.ax.get_position().x0 + + assert np.isclose( + [cx1, cx0], + [x1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + x0 * shrink] + ).all() From d1e62661150d8f3a351848cbe9010c5684e7ed22 Mon Sep 17 00:00:00 2001 From: shawnchen Date: Sat, 26 Sep 2020 15:00:16 +0800 Subject: [PATCH 05/16] rewrite test and factored out duplicate code --- lib/matplotlib/colorbar.py | 74 +++++++++++---------------- lib/matplotlib/tests/test_colorbar.py | 8 +-- 2 files changed, 33 insertions(+), 49 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 896c9a3eb068..ce496699f999 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1564,62 +1564,46 @@ def make_axes_gridspec(parent, *, location=None, orientation=None, kw['orientation'] = loc_settings['orientation'] location = kw['ticklocation'] = loc_settings['location'] - pad = loc_settings["pad"] + anchor = kw.pop('anchor', loc_settings['anchor']) + panchor = kw.pop('panchor', loc_settings['panchor']) + pad = kw.pop('pad', loc_settings["pad"]) + wh_space = 2 * pad / (1 - pad) # we need to none the tree of layoutboxes because constrained_layout can't # remove and replace the tree hierarchy w/o a segfault. layoutbox.nonetree(parent.get_subplotspec().get_gridspec()._layoutbox) - if location == "left": - wh_space = 2 * pad / (1 - pad) - anchor = kw.pop('anchor', (0.0, 0.5)) - panchor = kw.pop('panchor', (1.0, 0.5)) - - # for shrinking - wh_ratios = [(1-anchor[1])*(1-shrink), shrink, anchor[1]*(1-shrink)] - - gs = parent.get_subplotspec().subgridspec( - 1, 2, wspace=wh_space, width_ratios=[fraction, 1-fraction-pad]) - ss_main = gs[1] - ss_cb = gs[0].subgridspec(3, 1, hspace=0, height_ratios=wh_ratios)[1] - elif location == "right": - wh_space = 2 * pad / (1 - pad) - anchor = kw.pop('anchor', (0.0, 0.5)) - panchor = kw.pop('panchor', (1.0, 0.5)) + if location in ('left', 'right'): # for shrinking wh_ratios = [(1-anchor[1])*(1-shrink), shrink, anchor[1]*(1-shrink)] - gs = parent.get_subplotspec().subgridspec( - 1, 2, wspace=wh_space, width_ratios=[1-fraction-pad, fraction]) - ss_main = gs[0] - ss_cb = gs[1].subgridspec(3, 1, hspace=0, height_ratios=wh_ratios)[1] - elif location == "top": - wh_space = 2 * pad / (1 - pad) - anchor = kw.pop('anchor', (0.5, 1.0)) - panchor = kw.pop('panchor', (0.5, 0.0)) - - # for shrinking - wh_ratios = [(1-anchor[0])*(1-shrink), shrink, anchor[0]*(1-shrink)] - - gs = parent.get_subplotspec().subgridspec( - 2, 1, hspace=wh_space, height_ratios=[fraction, 1-fraction-pad]) - ss_main = gs[1] - ss_cb = gs[0].subgridspec(1, 3, wspace=0, width_ratios=wh_ratios)[1] - aspect = 1 / aspect - else: # "bottom" - wh_space = 2 * pad / (1 - pad) - anchor = kw.pop('anchor', (0.5, 1.0)) - panchor = kw.pop('panchor', (0.5, 0.0)) - + if location == 'left': + gs = parent.get_subplotspec().subgridspec( + 1, 2, wspace=wh_space, width_ratios=[fraction, 1-fraction-pad]) + ss_main = gs[1] + ss_cb = gs[0].subgridspec(3, 1, hspace=0, height_ratios=wh_ratios)[1] + else: + gs = parent.get_subplotspec().subgridspec( + 1, 2, wspace=wh_space, width_ratios=[1-fraction-pad, fraction]) + ss_main = gs[0] + ss_cb = gs[1].subgridspec(3, 1, hspace=0, height_ratios=wh_ratios)[1] + else: # for shrinking wh_ratios = [anchor[0]*(1-shrink), shrink, (1-anchor[0])*(1-shrink)] - gs = parent.get_subplotspec().subgridspec( - 2, 1, hspace=wh_space, height_ratios=[1-fraction-pad, fraction]) - ss_main = gs[0] - ss_cb = gs[1].subgridspec(1, 3, wspace=0, width_ratios=wh_ratios)[1] - aspect = 1 / aspect - + if location == 'bottom': + gs = parent.get_subplotspec().subgridspec( + 2, 1, hspace=wh_space, height_ratios=[1-fraction-pad, fraction]) + ss_main = gs[0] + ss_cb = gs[1].subgridspec(1, 3, wspace=0, width_ratios=wh_ratios)[1] + aspect = 1 / aspect + else: + gs = parent.get_subplotspec().subgridspec( + 2, 1, hspace=wh_space, height_ratios=[fraction, 1-fraction-pad]) + ss_main = gs[1] + ss_cb = gs[0].subgridspec(1, 3, wspace=0, width_ratios=wh_ratios)[1] + aspect = 1 / aspect + parent.set_subplotspec(ss_main) parent.update_params() parent._set_position(parent.figbox) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 5d5d649f9b50..10700b03588e 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -644,10 +644,10 @@ def test_anchored_cbar_position_using_specgrid(): cy1 = cbar.ax.get_position().y1 cy0 = cbar.ax.get_position().y0 - assert np.isclose( + np.testing.assert_allclose( [cy1, cy0], [y1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + y0 * shrink] - ).all() + ) # horizontal shrink = 0.5 @@ -666,7 +666,7 @@ def test_anchored_cbar_position_using_specgrid(): cx1 = cbar.ax.get_position().x1 cx0 = cbar.ax.get_position().x0 - assert np.isclose( + np.testing.assert_allclose( [cx1, cx0], [x1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + x0 * shrink] - ).all() + ) From a4e32ee9cdc28634cb1836dc2e5a8fe4ab023da6 Mon Sep 17 00:00:00 2001 From: shawnchen Date: Sat, 26 Sep 2020 16:10:50 +0800 Subject: [PATCH 06/16] modify the gallery example of colorbar --- examples/color/colorbar_basics.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/color/colorbar_basics.py b/examples/color/colorbar_basics.py index 64bed1449da3..7397b3a4d8bd 100644 --- a/examples/color/colorbar_basics.py +++ b/examples/color/colorbar_basics.py @@ -32,8 +32,9 @@ fig.colorbar(pos, ax=ax1) # repeat everything above for the negative data +# you can specifiy location, anchor and shrink the colorbar neg = ax2.imshow(Zneg, cmap='Reds_r', interpolation='none') -fig.colorbar(neg, ax=ax2) +fig.colorbar(neg, ax=ax2, location='right', anchor=(0,0.3), shrink=0.7) # Plot both positive and negative values between +/- 1.2 pos_neg_clipped = ax3.imshow(Z, cmap='RdBu', vmin=-1.2, vmax=1.2, From 979ecee041b6c724ffef017ae580326fd239e2d1 Mon Sep 17 00:00:00 2001 From: shawnchen Date: Sat, 26 Sep 2020 16:39:07 +0800 Subject: [PATCH 07/16] flake8 compliance --- examples/color/colorbar_basics.py | 2 +- lib/matplotlib/colorbar.py | 30 +++++++++++++++++------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/examples/color/colorbar_basics.py b/examples/color/colorbar_basics.py index 7397b3a4d8bd..c4f52cbff94c 100644 --- a/examples/color/colorbar_basics.py +++ b/examples/color/colorbar_basics.py @@ -34,7 +34,7 @@ # repeat everything above for the negative data # you can specifiy location, anchor and shrink the colorbar neg = ax2.imshow(Zneg, cmap='Reds_r', interpolation='none') -fig.colorbar(neg, ax=ax2, location='right', anchor=(0,0.3), shrink=0.7) +fig.colorbar(neg, ax=ax2, location='right', anchor=(0, 0.3), shrink=0.7) # Plot both positive and negative values between +/- 1.2 pos_neg_clipped = ax3.imshow(Z, cmap='RdBu', vmin=-1.2, vmax=1.2, diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index ba97084f4144..47c6523c5adc 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1523,32 +1523,36 @@ def make_axes_gridspec(parent, *, location=None, orientation=None, wh_ratios = [(1-anchor[1])*(1-shrink), shrink, anchor[1]*(1-shrink)] if location == 'left': - gs = parent.get_subplotspec().subgridspec( - 1, 2, wspace=wh_space, width_ratios=[fraction, 1-fraction-pad]) + gs = parent.get_subplotspec().subgridspec(1, 2, + wspace=wh_space, width_ratios=[fraction, 1-fraction-pad]) ss_main = gs[1] - ss_cb = gs[0].subgridspec(3, 1, hspace=0, height_ratios=wh_ratios)[1] + ss_cb = gs[0].subgridspec(3, 1, + hspace=0, height_ratios=wh_ratios)[1] else: - gs = parent.get_subplotspec().subgridspec( - 1, 2, wspace=wh_space, width_ratios=[1-fraction-pad, fraction]) + gs = parent.get_subplotspec().subgridspec(1, 2, + wspace=wh_space, width_ratios=[1-fraction-pad, fraction]) ss_main = gs[0] - ss_cb = gs[1].subgridspec(3, 1, hspace=0, height_ratios=wh_ratios)[1] + ss_cb = gs[1].subgridspec(3, 1, + hspace=0, height_ratios=wh_ratios)[1] else: # for shrinking wh_ratios = [anchor[0]*(1-shrink), shrink, (1-anchor[0])*(1-shrink)] if location == 'bottom': - gs = parent.get_subplotspec().subgridspec( - 2, 1, hspace=wh_space, height_ratios=[1-fraction-pad, fraction]) + gs = parent.get_subplotspec().subgridspec(2, 1, + hspace=wh_space, height_ratios=[1-fraction-pad, fraction]) ss_main = gs[0] - ss_cb = gs[1].subgridspec(1, 3, wspace=0, width_ratios=wh_ratios)[1] + ss_cb = gs[1].subgridspec(1, 3, + wspace=0, width_ratios=wh_ratios)[1] aspect = 1 / aspect else: - gs = parent.get_subplotspec().subgridspec( - 2, 1, hspace=wh_space, height_ratios=[fraction, 1-fraction-pad]) + gs = parent.get_subplotspec().subgridspec(2, 1, + hspace=wh_space, height_ratios=[fraction, 1-fraction-pad]) ss_main = gs[1] - ss_cb = gs[0].subgridspec(1, 3, wspace=0, width_ratios=wh_ratios)[1] + ss_cb = gs[0].subgridspec(1, 3, + wspace=0, width_ratios=wh_ratios)[1] aspect = 1 / aspect - + parent.set_subplotspec(ss_main) parent.set_anchor(loc_settings["panchor"]) From 936576705b04319ea4715866e9f30151e91e3957 Mon Sep 17 00:00:00 2001 From: shawnchen Date: Sat, 26 Sep 2020 20:52:49 +0800 Subject: [PATCH 08/16] flake8 compliance --- lib/matplotlib/colorbar.py | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 47c6523c5adc..55e2cb3a5f46 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -44,6 +44,7 @@ import matplotlib.path as mpath import matplotlib.spines as mspines import matplotlib.transforms as mtransforms +import matplotlib._layoutbox as layoutbox from matplotlib import docstring _log = logging.getLogger(__name__) @@ -1523,34 +1524,38 @@ def make_axes_gridspec(parent, *, location=None, orientation=None, wh_ratios = [(1-anchor[1])*(1-shrink), shrink, anchor[1]*(1-shrink)] if location == 'left': - gs = parent.get_subplotspec().subgridspec(1, 2, - wspace=wh_space, width_ratios=[fraction, 1-fraction-pad]) + gs = parent.get_subplotspec().subgridspec( + 1, 2, wspace=wh_space, + width_ratios=[fraction, 1-fraction-pad]) ss_main = gs[1] - ss_cb = gs[0].subgridspec(3, 1, - hspace=0, height_ratios=wh_ratios)[1] + ss_cb = gs[0].subgridspec( + 3, 1, hspace=0, height_ratios=wh_ratios)[1] else: - gs = parent.get_subplotspec().subgridspec(1, 2, - wspace=wh_space, width_ratios=[1-fraction-pad, fraction]) + gs = parent.get_subplotspec().subgridspec( + 1, 2, wspace=wh_space, + width_ratios=[1-fraction-pad, fraction]) ss_main = gs[0] - ss_cb = gs[1].subgridspec(3, 1, - hspace=0, height_ratios=wh_ratios)[1] + ss_cb = gs[1].subgridspec( + 3, 1, hspace=0, height_ratios=wh_ratios)[1] else: # for shrinking wh_ratios = [anchor[0]*(1-shrink), shrink, (1-anchor[0])*(1-shrink)] if location == 'bottom': - gs = parent.get_subplotspec().subgridspec(2, 1, - hspace=wh_space, height_ratios=[1-fraction-pad, fraction]) + gs = parent.get_subplotspec().subgridspec( + 2, 1, hspace=wh_space, + height_ratios=[1-fraction-pad, fraction]) ss_main = gs[0] - ss_cb = gs[1].subgridspec(1, 3, - wspace=0, width_ratios=wh_ratios)[1] + ss_cb = gs[1].subgridspec( + 1, 3, wspace=0, width_ratios=wh_ratios)[1] aspect = 1 / aspect else: - gs = parent.get_subplotspec().subgridspec(2, 1, - hspace=wh_space, height_ratios=[fraction, 1-fraction-pad]) + gs = parent.get_subplotspec().subgridspec( + 2, 1, hspace=wh_space, + height_ratios=[fraction, 1-fraction-pad]) ss_main = gs[1] - ss_cb = gs[0].subgridspec(1, 3, - wspace=0, width_ratios=wh_ratios)[1] + ss_cb = gs[0].subgridspec( + 1, 3, wspace=0, width_ratios=wh_ratios)[1] aspect = 1 / aspect parent.set_subplotspec(ss_main) From 74f5f4853af08f21022ebfcc05e0071e328e274b Mon Sep 17 00:00:00 2001 From: shawnchen Date: Sun, 27 Sep 2020 21:37:25 +0800 Subject: [PATCH 09/16] add different senarioes for anchored_cbar_position_using_specgrid --- lib/matplotlib/colorbar.py | 5 -- lib/matplotlib/tests/test_colorbar.py | 73 +++++++++++++++++++-------- 2 files changed, 53 insertions(+), 25 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 55e2cb3a5f46..73886dabff77 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -44,7 +44,6 @@ import matplotlib.path as mpath import matplotlib.spines as mspines import matplotlib.transforms as mtransforms -import matplotlib._layoutbox as layoutbox from matplotlib import docstring _log = logging.getLogger(__name__) @@ -1515,10 +1514,6 @@ def make_axes_gridspec(parent, *, location=None, orientation=None, pad = kw.pop('pad', loc_settings["pad"]) wh_space = 2 * pad / (1 - pad) - # we need to none the tree of layoutboxes because constrained_layout can't - # remove and replace the tree hierarchy w/o a segfault. - layoutbox.nonetree(parent.get_subplotspec().get_gridspec()._layoutbox) - if location in ('left', 'right'): # for shrinking wh_ratios = [(1-anchor[1])*(1-shrink), shrink, anchor[1]*(1-shrink)] diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index d7e58aa42fe7..50286e020e55 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -629,44 +629,77 @@ def test_anchored_cbar_position_using_specgrid(): levels = [0, 200, 400, 600, 800, 1000, 1200] shrink = 0.5 anchor_y = 0.3 - # vertival + # right fig, ax = plt.subplots() cs = ax.contourf(data, levels=levels) cbar = plt.colorbar( cs, ax=ax, use_gridspec=True, - orientation='vertical', anchor=(1, anchor_y), shrink=shrink) + location='right', anchor=(1, anchor_y), shrink=shrink) - # y1: the top of ax, y0: the bottom of ax, p0: the y postion of anchor - # cy1 : the top of colorbar ax, cy0: the bottom of colorbar ax - y1 = ax.get_position().y1 - y0 = ax.get_position().y0 + # the bottom left corner of one ax is (x0, y0) + # the top right corner of one ax is (x1, y1) + # p0: the verticle / horizontal postion of anchor + x0, y0, x1, y1 = ax.get_position().extents + cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents p0 = (y1 - y0) * anchor_y + y0 - cy1 = cbar.ax.get_position().y1 - cy0 = cbar.ax.get_position().y0 np.testing.assert_allclose( [cy1, cy0], - [y1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + y0 * shrink] - ) + [y1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + y0 * shrink]) - # horizontal + # left + fig, ax = plt.subplots() + cs = ax.contourf(data, levels=levels) + cbar = plt.colorbar( + cs, ax=ax, use_gridspec=True, + location='left', anchor=(1, anchor_y), shrink=shrink) + + # the bottom left corner of one ax is (x0, y0) + # the top right corner of one ax is (x1, y1) + # p0: the verticle / horizontal postion of anchor + x0, y0, x1, y1 = ax.get_position().extents + cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents + p0 = (y1 - y0) * anchor_y + y0 + + np.testing.assert_allclose( + [cy1, cy0], + [y1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + y0 * shrink]) + # top + shrink = 0.5 + anchor_x = 0.3 + fig, ax = plt.subplots() + cs = ax.contourf(data, levels=levels) + cbar = plt.colorbar( + cs, ax=ax, use_gridspec=True, + location='top', anchor=(anchor_x, 1), shrink=shrink) + + # the bottom left corner of one ax is (x0, y0) + # the top right corner of one ax is (x1, y1) + # p0: the verticle / horizontal postion of anchor + x0, y0, x1, y1 = ax.get_position().extents + cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents + p0 = (x1 - x0) * anchor_x + x0 + + np.testing.assert_allclose( + [cx1, cx0], + [x1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + x0 * shrink]) + + # bottom shrink = 0.5 anchor_x = 0.3 fig, ax = plt.subplots() cs = ax.contourf(data, levels=levels) cbar = plt.colorbar( cs, ax=ax, use_gridspec=True, - orientation='horizontal', anchor=(anchor_x, 1), shrink=shrink) + location='bottom', anchor=(anchor_x, 1), shrink=shrink) - # x1: the right of ax, x0: the left of ax, p0: the x postion of anchor - # cx1 : the right of colorbar ax, cx0: the left of colorbar ax - x1 = ax.get_position().x1 - x0 = ax.get_position().x0 + # the bottom left corner of one ax is (x0, y0) + # the top right corner of one ax is (x1, y1) + # p0: the verticle / horizontal postion of anchor + x0, y0, x1, y1 = ax.get_position().extents + cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents p0 = (x1 - x0) * anchor_x + x0 - cx1 = cbar.ax.get_position().x1 - cx0 = cbar.ax.get_position().x0 np.testing.assert_allclose( [cx1, cx0], - [x1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + x0 * shrink] - ) + [x1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + x0 * shrink]) From af63301dff540d8c9996b1463e4e6d525537fd4c Mon Sep 17 00:00:00 2001 From: ShawnChen1996 Date: Fri, 2 Oct 2020 23:13:41 +0800 Subject: [PATCH 10/16] Update examples/color/colorbar_basics.py Co-authored-by: Elliott Sales de Andrade --- examples/color/colorbar_basics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/color/colorbar_basics.py b/examples/color/colorbar_basics.py index c4f52cbff94c..e1815b1da6f3 100644 --- a/examples/color/colorbar_basics.py +++ b/examples/color/colorbar_basics.py @@ -32,7 +32,7 @@ fig.colorbar(pos, ax=ax1) # repeat everything above for the negative data -# you can specifiy location, anchor and shrink the colorbar +# you can specify location, anchor and shrink the colorbar neg = ax2.imshow(Zneg, cmap='Reds_r', interpolation='none') fig.colorbar(neg, ax=ax2, location='right', anchor=(0, 0.3), shrink=0.7) From 762f61bf1a75c3167e88ddbfffe208d10fb6dfb1 Mon Sep 17 00:00:00 2001 From: ShawnChen1996 Date: Fri, 2 Oct 2020 23:14:09 +0800 Subject: [PATCH 11/16] Update lib/matplotlib/tests/test_colorbar.py Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/tests/test_colorbar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 50286e020e55..60c2237d2ada 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -638,7 +638,7 @@ def test_anchored_cbar_position_using_specgrid(): # the bottom left corner of one ax is (x0, y0) # the top right corner of one ax is (x1, y1) - # p0: the verticle / horizontal postion of anchor + # p0: the vertical / horizontal position of anchor x0, y0, x1, y1 = ax.get_position().extents cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents p0 = (y1 - y0) * anchor_y + y0 From 682082a3ca7cec2c88e98f80d200fec3d082e437 Mon Sep 17 00:00:00 2001 From: ShawnChen1996 Date: Fri, 2 Oct 2020 23:14:26 +0800 Subject: [PATCH 12/16] Update lib/matplotlib/tests/test_colorbar.py Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/tests/test_colorbar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 60c2237d2ada..7686dd7458a6 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -656,7 +656,7 @@ def test_anchored_cbar_position_using_specgrid(): # the bottom left corner of one ax is (x0, y0) # the top right corner of one ax is (x1, y1) - # p0: the verticle / horizontal postion of anchor + # p0: the vertical / horizontal position of anchor x0, y0, x1, y1 = ax.get_position().extents cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents p0 = (y1 - y0) * anchor_y + y0 From 0548c973ea6f13090aa7a7a7202c5a60cbf78aa9 Mon Sep 17 00:00:00 2001 From: ShawnChen1996 Date: Fri, 2 Oct 2020 23:14:44 +0800 Subject: [PATCH 13/16] Update lib/matplotlib/tests/test_colorbar.py Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/tests/test_colorbar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 7686dd7458a6..5615094c1f9c 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -675,7 +675,7 @@ def test_anchored_cbar_position_using_specgrid(): # the bottom left corner of one ax is (x0, y0) # the top right corner of one ax is (x1, y1) - # p0: the verticle / horizontal postion of anchor + # p0: the vertical / horizontal position of anchor x0, y0, x1, y1 = ax.get_position().extents cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents p0 = (x1 - x0) * anchor_x + x0 From 003ecf612458959dee96d0914160baef5e9e6e6a Mon Sep 17 00:00:00 2001 From: ShawnChen1996 Date: Fri, 2 Oct 2020 23:14:58 +0800 Subject: [PATCH 14/16] Update lib/matplotlib/tests/test_colorbar.py Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/tests/test_colorbar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 5615094c1f9c..956b850a1c46 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -695,7 +695,7 @@ def test_anchored_cbar_position_using_specgrid(): # the bottom left corner of one ax is (x0, y0) # the top right corner of one ax is (x1, y1) - # p0: the verticle / horizontal postion of anchor + # p0: the vertical / horizontal position of anchor x0, y0, x1, y1 = ax.get_position().extents cx0, cy0, cx1, cy1 = cbar.ax.get_position().extents p0 = (x1 - x0) * anchor_x + x0 From 7e0207c932b33e63c4f046cb0720746b08222196 Mon Sep 17 00:00:00 2001 From: ShawnChen1996 Date: Fri, 2 Oct 2020 23:15:18 +0800 Subject: [PATCH 15/16] Update lib/matplotlib/tests/test_colorbar.py Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/tests/test_colorbar.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 956b850a1c46..3e9d14e4643f 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -664,6 +664,7 @@ def test_anchored_cbar_position_using_specgrid(): np.testing.assert_allclose( [cy1, cy0], [y1 * shrink + (1 - shrink) * p0, p0 * (1 - shrink) + y0 * shrink]) + # top shrink = 0.5 anchor_x = 0.3 From 6e117ff86af3fbc339e23c4b799a58568fdd975b Mon Sep 17 00:00:00 2001 From: shawnchen Date: Sat, 10 Oct 2020 22:16:02 +0800 Subject: [PATCH 16/16] rename wh_ratios to height_ratios/width_ratios --- lib/matplotlib/colorbar.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 73886dabff77..5f329d6ed4f5 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1516,7 +1516,8 @@ def make_axes_gridspec(parent, *, location=None, orientation=None, if location in ('left', 'right'): # for shrinking - wh_ratios = [(1-anchor[1])*(1-shrink), shrink, anchor[1]*(1-shrink)] + height_ratios = [ + (1-anchor[1])*(1-shrink), shrink, anchor[1]*(1-shrink)] if location == 'left': gs = parent.get_subplotspec().subgridspec( @@ -1524,17 +1525,18 @@ def make_axes_gridspec(parent, *, location=None, orientation=None, width_ratios=[fraction, 1-fraction-pad]) ss_main = gs[1] ss_cb = gs[0].subgridspec( - 3, 1, hspace=0, height_ratios=wh_ratios)[1] + 3, 1, hspace=0, height_ratios=height_ratios)[1] else: gs = parent.get_subplotspec().subgridspec( 1, 2, wspace=wh_space, width_ratios=[1-fraction-pad, fraction]) ss_main = gs[0] ss_cb = gs[1].subgridspec( - 3, 1, hspace=0, height_ratios=wh_ratios)[1] + 3, 1, hspace=0, height_ratios=height_ratios)[1] else: # for shrinking - wh_ratios = [anchor[0]*(1-shrink), shrink, (1-anchor[0])*(1-shrink)] + width_ratios = [ + anchor[0]*(1-shrink), shrink, (1-anchor[0])*(1-shrink)] if location == 'bottom': gs = parent.get_subplotspec().subgridspec( @@ -1542,7 +1544,7 @@ def make_axes_gridspec(parent, *, location=None, orientation=None, height_ratios=[1-fraction-pad, fraction]) ss_main = gs[0] ss_cb = gs[1].subgridspec( - 1, 3, wspace=0, width_ratios=wh_ratios)[1] + 1, 3, wspace=0, width_ratios=width_ratios)[1] aspect = 1 / aspect else: gs = parent.get_subplotspec().subgridspec( @@ -1550,7 +1552,7 @@ def make_axes_gridspec(parent, *, location=None, orientation=None, height_ratios=[fraction, 1-fraction-pad]) ss_main = gs[1] ss_cb = gs[0].subgridspec( - 1, 3, wspace=0, width_ratios=wh_ratios)[1] + 1, 3, wspace=0, width_ratios=width_ratios)[1] aspect = 1 / aspect parent.set_subplotspec(ss_main)