Skip to content

Commit c118f84

Browse files
committed
DOC: improve docs a bit
1 parent 114e8d3 commit c118f84

File tree

2 files changed

+35
-45
lines changed

2 files changed

+35
-45
lines changed

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
Axes used to make colorbar now wrapped
2-
======================================
3-
4-
The axes used to place a colorbar is now wrapped by a new parent class
5-
(``ColorbarAxes``) when the colorbar is created::
6-
7-
cb = fig.colorbar(im, cax=cax)
8-
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``.
12-
131
Colorbar lines no longer clipped
142
================================
153

lib/matplotlib/colorbar.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ def draw(self, renderer):
228228
return ret
229229

230230

231-
class _ColorbarLocator:
231+
class _ColorbarAxesLocator:
232+
"""
233+
Wrap any locator on the axes, or its position, to shrink the
234+
inner axes if there are extend triangles at either min or max.
235+
"""
232236
def __init__(self, cbar):
233237
"""
234238
*bounds* (a ``[l, b, w, h]`` rectangle) and *transform* together
@@ -248,36 +252,39 @@ def __call__(self, ax, renderer):
248252
lim = self._cbar._long_axis().get_view_interval()
249253
self._cbar._short_axis().set_view_interval(*lim)
250254

251-
pos = ax.get_position(original=True)
252255
if self._orig_locator is not None:
253256
pos = self._orig_locator(ax, renderer)
257+
else:
258+
pos = ax.get_position(original=True)
254259
if self._cbar.extend == 'neither':
255260
return pos
261+
262+
y, extendlen = self._cbar._proportional_y()
263+
if not self._cbar._extend_lower():
264+
extendlen[0] = 0
265+
if not self._cbar._extend_upper():
266+
extendlen[1] = 0
267+
len = sum(extendlen) + 1
268+
shrink = 1 / len
269+
offset = extendlen[0] / len
270+
# we need to reset the aspect ratio of the axes to account
271+
# of the extends...
272+
if not self._cbar._userax:
273+
aspect = ax._colorbar_info['aspect']
256274
else:
257-
y, extendlen = self._cbar._proportional_y()
258-
if not self._cbar._extend_lower():
259-
extendlen[0] = 0
260-
if not self._cbar._extend_upper():
261-
extendlen[1] = 0
262-
len = sum(extendlen) + 1
263-
shrink = 1 / len
264-
offset = extendlen[0] / len
265-
# we need to reset the aspect ratio of the axes to account
266-
# of the extends...
267-
if not self._cbar._userax:
268-
aspect = ax._colorbar_info['aspect']
269-
else:
270-
aspect = False
271-
if self._cbar.orientation == 'vertical':
272-
if aspect:
273-
ax.set_aspect(aspect*shrink)
274-
offset = offset * pos.height
275-
pos = pos.shrunk(1, shrink).translated(0, offset)
276-
else:
277-
if aspect:
278-
ax.set_aspect(aspect/shrink)
279-
offset = offset * pos.width
280-
pos = pos.shrunk(shrink, 1).translated(offset, 0)
275+
aspect = False
276+
# now shrink and/or offset to take into account the
277+
# extend tri/rectangles.
278+
if self._cbar.orientation == 'vertical':
279+
if aspect:
280+
ax.set_aspect(aspect*shrink)
281+
offset = offset * pos.height
282+
pos = pos.shrunk(1, shrink).translated(0, offset)
283+
else:
284+
if aspect:
285+
ax.set_aspect(aspect/shrink)
286+
offset = offset * pos.width
287+
pos = pos.shrunk(shrink, 1).translated(offset, 0)
281288
return pos
282289

283290

@@ -421,7 +428,7 @@ def __init__(self, ax, mappable=None, *, cmap=None,
421428

422429
self.ax = ax
423430
self._userax = userax
424-
self.ax._axes_locator = _ColorbarLocator(self)
431+
self.ax._axes_locator = _ColorbarAxesLocator(self)
425432
ax.set(navigate=False)
426433

427434
if extend is None:
@@ -606,8 +613,7 @@ def _add_solids_patches(self, X, Y, C, mappable):
606613

607614
def _do_extends(self, extendlen):
608615
"""
609-
Make adjustments of the inner axes for the extend triangles (or
610-
rectanges) and add them as patches.
616+
Add the extend tri/rectangles on the outside of the axes.
611617
"""
612618
# extend lengths are fraction of the *inner* part of colorbar,
613619
# not the total colorbar:
@@ -767,10 +773,6 @@ def add_lines(self, *args, **kwargs):
767773
self.ax.add_collection(col)
768774
self.stale = True
769775

770-
def set_scale(self, scale):
771-
self.ax.set_xscale(scale)
772-
self.ax.set_yscale(scale)
773-
774776
def update_ticks(self):
775777
"""
776778
Setup the ticks and ticklabels. This should not be needed by users.

0 commit comments

Comments
 (0)