Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 439a025

Browse files
committed
TST: and a few fixes
1 parent 7bc8954 commit 439a025

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

doc/api/next_api_changes/behavior/20054-JMK.rst

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,9 @@ The axes used to place a colorbar is now wrapped by a new parent class
66

77
cb = fig.colorbar(im, cax=cax)
88

9-
Now ``cb.ax`` returns the parent ``ColorbarAxes``, and to get
10-
``cax`` back, you can do::
11-
12-
cb.ax.parent_axes
13-
14-
The parent axes (``cb.ax.parent_axes``) handles the
15-
placement of the colorbar axes object, and its aspect ratio, but the child
16-
axes (``cb.ax``) handles all the tick parameters, labelling, and holds the
17-
artists that make up the colorbar. We have attempted to dispatch the
18-
appropriate colorbar methods to the appropriate axes.
9+
This means that ``cb.ax`` is no longer the same object as ``cax``. However,
10+
we map all the methods from ``cb.ax`` onto ``cax`` so ``cax`` should remain
11+
functionally the same as ``cb.ax``.
1912

2013
Colorbar lines no longer clipped
2114
================================

lib/matplotlib/colorbar.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ class ColorbarBase:
377377
extendrec
378378
379379
label : str
380+
381+
userax : boolean
382+
Whether the user created the axes or not. Default True
380383
"""
381384

382385
n_rasterize = 50 # rasterize solids if number of colors >= n_rasterize
@@ -398,7 +401,7 @@ def __init__(self, ax, cmap=None,
398401
extendfrac=None,
399402
extendrect=False,
400403
label='',
401-
userax=False,
404+
userax=True,
402405
):
403406
_api.check_isinstance([colors.Colormap, None], cmap=cmap)
404407
_api.check_in_list(

lib/matplotlib/figure.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,25 +1158,23 @@ def colorbar(self, mappable, cax=None, ax=None, use_gridspec=True, **kw):
11581158
# Store the value of gca so that we can set it back later on.
11591159
if cax is None:
11601160
current_ax = self.gca()
1161-
userax = False
1161+
kw['userax'] = False
11621162
if (use_gridspec and isinstance(ax, SubplotBase)
11631163
and not self.get_constrained_layout()):
11641164
cax, kw = cbar.make_axes_gridspec(ax, **kw)
11651165
else:
11661166
cax, kw = cbar.make_axes(ax, **kw)
11671167
else:
1168-
userax = True
1168+
kw['userax'] = True
11691169

11701170
# need to remove kws that cannot be passed to Colorbar
11711171
NON_COLORBAR_KEYS = ['fraction', 'pad', 'shrink', 'aspect', 'anchor',
11721172
'panchor']
11731173
cb_kw = {k: v for k, v in kw.items() if k not in NON_COLORBAR_KEYS}
1174-
if userax:
1175-
cb_kw['userax'] = True
1176-
1174+
11771175
cb = cbar.Colorbar(cax, mappable, **cb_kw)
11781176

1179-
if not userax:
1177+
if not kw['userax']:
11801178
self.sca(current_ax)
11811179
self.stale = True
11821180
return cb

lib/matplotlib/tests/test_colorbar.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from matplotlib.colorbar import ColorbarBase
1313
from matplotlib.ticker import FixedLocator
1414

15+
from matplotlib.testing.decorators import check_figures_equal
16+
1517

1618
def _get_cmap_norms():
1719
"""
@@ -724,3 +726,20 @@ def test_colorbar_change_lim_scale():
724726
pc = ax[1].pcolormesh(np.arange(100).reshape(10, 10)+1)
725727
cb = fig.colorbar(pc, ax=ax[1], extend='both')
726728
cb.ax.set_ylim([20, 90])
729+
730+
731+
@check_figures_equal(extensions=["png"])
732+
def test_axes_handles_same_functions(fig_ref, fig_test):
733+
# prove that cax and cb.ax are functionally the same
734+
for nn, fig in enumerate([fig_ref, fig_test]):
735+
ax = fig.add_subplot()
736+
pc = ax.pcolormesh(np.ones(300).reshape(10, 30))
737+
cax = fig.add_axes([0.9, 0.1, 0.03, 0.8])
738+
cb = fig.colorbar(pc, cax=cax)
739+
if nn == 0:
740+
caxx = cax
741+
else:
742+
caxx = cb.ax
743+
caxx.set_yticks(np.arange(20))
744+
caxx.set_yscale('log')
745+
caxx.set_position([0.92, 0.1, 0.02, 0.7])

0 commit comments

Comments
 (0)