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

Skip to content

Commit 6f88ba3

Browse files
committed
FIX: address review
1 parent abd1aa6 commit 6f88ba3

File tree

4 files changed

+36
-57
lines changed

4 files changed

+36
-57
lines changed

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,20 @@ Axes used to make colorbar now wrapped
22
======================================
33

44
The axes used to place a colorbar is now wrapped by a new parent class
5-
(``ColorbarAxes``). This affects calls like::
5+
when the colorbar is instnatiated ``ColorbarAxes``::
66

77
cb = fig.colorbar(im, cax=cax)
8-
cax.tick_params()
98

10-
so that this will no longer affect the visible axes ticks. Rather call
11-
methods on the axes stored on the colorbar object::
12-
13-
cb = fig.colorbar(im, cax=cax)
14-
cb.ax.tick_params()
15-
16-
If necessary, the original axes can be accessed via::
9+
So now ``cb.ax`` returns the parent ``ColorbarAxes`` and to get
10+
``cax`` back, you can do::
1711

1812
assert cb.ax.parent_axes == cax
1913

20-
This change means that the parent axes (``cb.ax.parent_axes``) handles the
14+
The parent axes (``cb.ax.parent_axes``) handles the
2115
placement of the colorbar axes object, and its aspect ratio, but the child
22-
axes (``cb.ax``) handles all the tick parameters, labelling, and holds the
23-
artists that make up the colorbar.
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.
2419

2520
Colorbar lines no longer clipped
2621
================================

lib/matplotlib/_constrained_layout.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,13 +448,15 @@ def _get_pos_and_bbox(ax, renderer):
448448
pos = pos.transformed(fig.transSubfigure - fig.transFigure)
449449
try:
450450
tightbbox = ax.get_tightbbox(renderer=renderer, for_layout_only=True)
451+
print('tight', tightbbox)
451452
except TypeError:
452453
tightbbox = ax.get_tightbbox(renderer=renderer)
453454

454455
if tightbbox is None:
455456
bbox = pos
456457
else:
457458
bbox = tightbbox.transformed(fig.transFigure.inverted())
459+
print('bbox', bbox)
458460
return pos, bbox
459461

460462

lib/matplotlib/colorbar.py

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ def _set_ticks_on_axis_warn(*args, **kw):
226226

227227
class ColorbarAxes(Axes):
228228
"""
229-
Colorbar Axes are actually two axes, a parent axes that takes care of
229+
ColorbarAxes packages two axes, a parent axes that takes care of
230230
positioning the axes, and an inset_axes that takes care of the drawing,
231231
labels, ticks, etc. The inset axes is used as a way to properly
232-
position the triangles (or rectangles) that are used to indicate
232+
position the extensions (triangles or rectangles) that are used to indicate
233233
over/under colors.
234234
235235
Users should not normally instantiate this class, but it is the class
@@ -251,13 +251,8 @@ def __init__(self, parent):
251251
# map some features to the parent so users have access...
252252
self.parent_ax.tick_params = self.inner_ax.tick_params
253253

254-
def get_position(self, original=False):
255-
# inherited
256-
return self.parent_ax.get_position(original=original)
257-
258-
def set_position(self, pos, which='both'):
259-
# inherited
260-
self.parent_ax.set_position(pos, which=which)
254+
for attr in ["get_position", "set_position", "set_aspect"]:
255+
setattr(self, attr, getattr(self.parent_ax, attr))
261256

262257
def _set_inner_bounds(self, bounds):
263258
"""
@@ -266,9 +261,6 @@ def _set_inner_bounds(self, bounds):
266261
self.inner_ax._axes_locator = _TransformedBoundsLocator(
267262
bounds, self.parent_ax.transAxes)
268263

269-
def set_aspect(self, aspect, **kwargs):
270-
self.parent_ax.set_aspect(aspect, **kwargs)
271-
272264

273265
class _ColorbarSpine(mspines.Spine):
274266
def __init__(self, axes):
@@ -566,16 +558,14 @@ def _do_extends(self, extendlen):
566558
"""
567559
# extend lengths are fraction of the *inner* part of colorbar,
568560
# not the total colorbar:
569-
el = extendlen[0] if self._extend_lower() else 0
570-
eu = extendlen[1] if self._extend_upper() else 0
571-
tot = eu + el + 1
572-
el = el / tot
573-
eu = eu / tot
561+
elower = extendlen[0] if self._extend_lower() else 0
562+
eupper = extendlen[1] if self._extend_upper() else 0
563+
tot = eupper + elower + 1
564+
elower = elower / tot
565+
eupper = eupper / tot
574566
width = 1 / tot
575567

576-
bounds = np.array([0.0, 0.0, 1.0, 1.0])
577-
bounds[1] = el
578-
bounds[3] = width
568+
bounds = np.array([0.0, elower, 1.0, width])
579569

580570
# make the inner axes smaller to make room for the extend rectangle
581571
top = bounds[1] + bounds[3]
@@ -605,9 +595,9 @@ def _do_extends(self, extendlen):
605595
hatches = [None]
606596
if self._extend_lower:
607597
if not self.extendrect:
608-
xy = np.array([[0.5, 0], [1, el], [0, el]])
598+
xy = np.array([[0.5, 0], [1, elower], [0, elower]])
609599
else:
610-
xy = np.array([[0, 0], [1., 0], [1, el], [0, el]])
600+
xy = np.array([[0, 0], [1., 0], [1, elower], [0, elower]])
611601
if self.orientation == 'horizontal':
612602
xy = xy[:, ::-1]
613603
color = self.cmap(self.norm(self._values[0]))
@@ -618,9 +608,9 @@ def _do_extends(self, extendlen):
618608
self.ax.parent_ax.add_patch(patch)
619609
if self._extend_upper:
620610
if not self.extendrect:
621-
xy = np.array([[0.5, 1], [1, 1-eu], [0, 1-eu]])
611+
xy = np.array([[0.5, 1], [1, 1-eupper], [0, 1-eupper]])
622612
else:
623-
xy = np.array([[0, 1], [1, 1], [1, 1-eu], [0, 1-eu]])
613+
xy = np.array([[0, 1], [1, 1], [1, 1-eupper], [0, 1-eupper]])
624614
if self.orientation == 'horizontal':
625615
xy = xy[:, ::-1]
626616
color = self.cmap(self.norm(self._values[-1]))
@@ -663,14 +653,14 @@ def add_lines(self, levels, colors, linewidths, erase=True):
663653
xy = np.stack([X, Y], axis=-1)
664654
else:
665655
xy = np.stack([Y, X], axis=-1)
666-
col = collections.LineCollection(xy, linewidths=linewidths)
656+
col = collections.LineCollection(xy, linewidths=linewidths,
657+
colors=colors)
667658

668659
if erase and self.lines:
669660
for lc in self.lines:
670661
lc.remove()
671662
self.lines = []
672663
self.lines.append(col)
673-
col.set_color(colors)
674664

675665
# make a clip path that is just a linewidth bigger than the axes...
676666
fac = np.max(linewidths) / 72
@@ -696,21 +686,9 @@ def update_ticks(self):
696686
ax = self.ax
697687
# Get the locator and formatter; defaults to self.locator if not None.
698688
self._get_ticker_locator_formatter()
699-
#if (self.boundaries is not None and self.spacing == 'uniform'):
700-
if False:
701-
_log.debug('Using fixed locator on colorbar')
702-
ticks, ticklabels, offset_string = self._ticker(self.locator,
703-
self.formatter)
704-
self._long_axis().set_ticks(ticks)
705-
self._long_axis().set_ticklabels(ticklabels)
706-
fmt = self._long_axis().get_major_formatter()
707-
fmt.set_offset_string(offset_string)
708-
else: # use auto locators...
709-
_log.debug('Using auto colorbar locator %r on colorbar',
710-
self.locator)
711-
self._long_axis().set_major_locator(self.locator)
712-
self._long_axis().set_minor_locator(self.minorlocator)
713-
self._long_axis().set_major_formatter(self.formatter)
689+
self._long_axis().set_major_locator(self.locator)
690+
self._long_axis().set_minor_locator(self.minorlocator)
691+
self._long_axis().set_major_formatter(self.formatter)
714692

715693
def _get_ticker_locator_formatter(self):
716694
"""
@@ -787,7 +765,8 @@ def set_ticklabels(self, ticklabels, update_ticks=True):
787765
"""
788766
Set tick labels.
789767
790-
update_ticks kwarg has no effect.
768+
update_ticks : bool, default: True
769+
This keyword argument is ignored and will be be removed.
791770
"""
792771
if isinstance(self.locator, ticker.FixedLocator):
793772
self.formatter = ticker.FixedFormatter(ticklabels)
@@ -797,7 +776,7 @@ def set_ticklabels(self, ticklabels, update_ticks=True):
797776

798777
def minorticks_on(self):
799778
"""
800-
Turn the minor ticks of the colorbar.
779+
Turn on colorbar minor ticks.
801780
"""
802781
self.ax.minorticks_on()
803782
self.minorlocator = self._long_axis().get_minor_locator()

lib/mpl_toolkits/tests/test_axes_grid.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22

33
import matplotlib as mpl
4+
import matplotlib.ticker as mticker
45
from matplotlib.testing.decorators import image_comparison
56
import matplotlib.pyplot as plt
67
from mpl_toolkits.axes_grid1 import ImageGrid
@@ -39,9 +40,11 @@ def test_imagegrid_cbar_mode_edge():
3940
ax3.imshow(np.abs(arr), cmap='jet')
4041
ax4.imshow(np.arctan2(arr.imag, arr.real), cmap='hsv')
4142

43+
# In each row/column, the "first" colorbars must be overwritten by the
44+
# "second" ones. To achieve this, clear out the axes first.
4245
for ax in grid:
4346
ax.cax.cla()
44-
cb = ax.cax.colorbar(ax.images[0]) # old default locator.
47+
cb = ax.cax.colorbar(ax.images[0])
4548

4649

4750
def test_imagegrid():
@@ -50,4 +53,4 @@ def test_imagegrid():
5053
ax = grid[0]
5154
im = ax.imshow([[1, 2]], norm=mpl.colors.LogNorm())
5255
cb = ax.cax.colorbar(im)
53-
# assert isinstance(cb.locator, mpl.colorbar._ColorbarLogLocator)
56+
assert isinstance(cb.locator, mticker.LogLocator)

0 commit comments

Comments
 (0)