8000 Merge pull request #15282 from anntzer/colorbar · matplotlib/matplotlib@4b27b46 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b27b46

Browse files
authored
Merge pull request #15282 from anntzer/colorbar
Colorbar cleanup.
2 parents f9e37f7 + 057aa19 commit 4b27b46

File tree

1 file changed

+33
-52
lines changed

1 file changed

+33
-52
lines changed

lib/matplotlib/colorbar.py

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
a function for resizing an axes and adding a second axes
1414
suitable for a colorbar
1515
16-
The :meth:`~matplotlib.figure.Figure.colorbar` method uses :func:`make_axes`
17-
and :class:`Colorbar`; the :func:`~matplotlib.pyplot.colorbar` function
18-
is a thin wrapper over :meth:`~matplotlib.figure.Figure.colorbar`.
19-
16+
The `~.Figure.colorbar` method uses `make_axes` and `Colorbar`; the
17+
`~.pyplot.colorbar` function is a thin wrapper over `~.Figure.colorbar`.
2018
'''
19+
2120
import copy
2221
import logging
2322

@@ -98,12 +97,9 @@
9897
If None, ticks are determined automatically from the
9998
input.
10099
*format* None or str or Formatter
101-
If None, the
102-
:class:`~matplotlib.ticker.ScalarFormatter` is used.
103-
If a format string is given, e.g., '%.3f', that is
104-
used. An alternative
105-
:class:`~matplotlib.ticker.Formatter` object may be
106-
given instead.
100+
If None, `~.ticker.ScalarFormatter` is used.
101+
If a format string is given, e.g., '%.3f', that is used.
102+
An alternative `~.ticker.Formatter` may be given instead.
107103
*drawedges* bool
108104
Whether to draw lines at color boundaries.
109105
============ ====================================================
@@ -130,8 +126,7 @@
130126
Add a colorbar to a plot.
131127
132128
Function signatures for the :mod:`~matplotlib.pyplot` interface; all
133-
but the first are also method signatures for the
134-
:meth:`~matplotlib.figure.Figure.colorbar` method::
129+
but the first are also method signatures for the `~.Figure.colorbar` method::
135130
136131
colorbar(**kwargs)
137132
colorbar(mappable, **kwargs)
@@ -180,8 +175,8 @@
180175
colorbar properties:
181176
%s
182177
183-
If *mappable* is a :class:`~matplotlib.contours.ContourSet`, its *extend*
184-
kwarg is included automatically.
178+
If *mappable* is a `~.contour.ContourSet`, its *extend* kwarg is included
179+
automatically.
185180
186181
The *shrink* kwarg provides a simple way to scale the colorbar with respect
187182
to the axes. Note that if *cax* is specified, it determines the size of the
@@ -422,10 +417,6 @@ class ColorbarBase(_ColorbarMappableDummy):
422417
423418
label : str
424419
"""
425-
_slice_dict = {'neither': slice(0, None),
426-
'both': slice(1, -1),
427-
'min': slice(1, None),
428-
'max': slice(0, -1)}
429420

430421
n_rasterize = 50 # rasterize solids if number of colors >= n_rasterize
431422

@@ -452,8 +443,6 @@ def __init__(self, ax, cmap=None,
452443
cbook._check_in_list(
453444
['auto', 'left', 'right', 'top', 'bottom'],
454445
ticklocation=ticklocation)
455-
cbook._check_in_list(
456-
self._slice_dict, extend=extend)
457446
cbook._check_in_list(
458447
['uniform', 'proportional'], spacing=spacing)
459448

@@ -469,7 +458,10 @@ def __init__(self, ax, cmap=None,
469458
self.values = values
470459
self.boundaries = boundaries
471460
self.extend = extend
472-
self._inside = self._slice_dict[extend]
461+
self._inside = cbook._check_getitem(
462+
{'neither': slice(0, None), 'both': slice(1, -1),
463+
'min': slice(1, None), 'max': slice(0, -1)},
464+
extend=extend)
473465
self.spacing = spacing
474466
self.orientation = orientation
475467
self.drawedges = drawedges
@@ -610,11 +602,9 @@ def _use_auto_colorbar_locator(self):
610602
Return if we should use an adjustable tick locator or a fixed
611603
one. (check is used twice so factored out here...)
612604
"""
613-
contouring = ((self.boundaries is not None) and
614-
(self.spacing == 'uniform'))
615-
return (((type(self.norm) == colors.Normalize)
616-
or (type(self.norm) == colors.LogNorm))
617-
and not contouring)
605+
contouring = self.boundaries is not None and self.spacing == 'uniform'
606+
return (type(self.norm) in [colors.Normalize, colors.LogNorm]
607+
and not contouring)
618608

619609
def _reset_locator_formatter_scale(self):
620610
"""
@@ -624,7 +614,7 @@ def _reset_locator_formatter_scale(self):
624614
"""
625615
self.locator = None
626616
self.formatter = None
627-
if (isinstance(self.norm, colors.LogNorm)):
617+
if isinstance(self.norm, colors.LogNorm):
628618
# *both* axes are made log so that determining the
629619
# mid point is easier.
630620
self.ax.set_xscale('log')
@@ -644,8 +634,7 @@ def update_ticks(self):
644634
locator, formatter = self._get_ticker_locator_formatter()
645635
long_axis = ax.yaxis if self.orientation == 'vertical' else ax.xaxis
646636
if self._use_auto_colorbar_locator():
647-
_log.debug('Using auto colorbar locator on colorbar')
648-
_log.debug('locator: %r', locator)
637+
_log.debug('Using auto colorbar locator %r on colorbar', locator)
649638
long_axis.set_major_locator(locator)
650639
long_axis.set_major_formatter(formatter)
651640
else:
@@ -803,7 +792,7 @@ def _edges(self, X, Y):
803792

804793
def _add_solids(self, X, Y, C):
805794
'''
806-
Draw the colors using :meth:`~matplotlib.axes.Axes.pcolormesh`;
795+
Draw the colors using `~.axes.Axes.pcolormesh`;
807796
optionally add separators.
808797
'''
809798
if self.orientation == 'vertical':
@@ -1175,25 +1164,21 @@ def set_alpha(self, alpha):
11751164

11761165
def remove(self):
11771166
"""
1178-
Remove this colorbar from the figure
1167+
Remove this colorbar from the figure.
11791168
"""
1180-
11811169
fig = self.ax.figure
11821170
fig.delaxes(self.ax)
11831171

11841172

11851173
class Colorbar(ColorbarBase):
11861174
"""
1187-
This class connects a :class:`ColorbarBase` to a
1188-
:class:`~matplotlib.cm.ScalarMappable` such as a
1189-
:class:`~matplotlib.image.AxesImage` generated via
1190-
:meth:`~matplotlib.axes.Axes.imshow`.
1191-
1192-
It is not intended to be instantiated directly; instead,
1193-
use :meth:`~matplotlib.figure.Figure.colorbar` or
1194-
:func:`~matplotlib.pyplot.colorbar` to make your colorbar.
1175+
This class connects a `ColorbarBase` to a `~.cm.ScalarMappable`
1176+
such as an `~.image.AxesImage` generated via `~.axes.Axes.imshow`.
11951177
1178+
It is not intended to be instantiated directly; instead, use
1179+
`~.figure.Figure.colorbar` or `~.pyplot.colorbar` to make your colorbar.
11961180
"""
1181+
11971182
def __init__(self, ax, mappable, **kw):
11981183
# Ensure the given mappable's norm has appropriate vmin and vmax set
11991184
# even if mappable.draw has not yet been called.
@@ -1230,15 +1215,13 @@ def on_mappable_changed(self, mappable):
12301215
12311216
Typically this is automatically registered as an event handler
12321217
by :func:`colorbar_factory` and should not be called manually.
1233-
12341218
"""
12351219
_log.debug('colorbar mappable changed')
12361220
self.update_normal(mappable)
12371221

12381222
def add_lines(self, CS, erase=True):
12391223
'''
1240-
Add the lines from a non-filled
1241-
:class:`~matplotlib.contour.ContourSet` to the colorbar.
1224+
Add the lines from a non-filled `~.contour.ContourSet` to the colorbar.
12421225
12431226
Set *erase* to False if these lines should be added to
12441227
any pre-existing lines.
@@ -1599,14 +1582,12 @@ def make_axes_gridspec(parent, *, fraction=0.15, shrink=1.0, aspect=20, **kw):
15991582

16001583
class ColorbarPatch(Colorbar):
16011584
"""
1602-
A Colorbar which is created using :class:`~matplotlib.patches.Patch`
1603-
rather than the default :func:`~matplotlib.axes.pcolor`.
1604-
1605-
It uses a list of Patch instances instead of a
1606-
:class:`~matplotlib.collections.PatchCollection` because the
1607-
latter does not allow the hatch pattern to vary among the
1585+
A Colorbar that uses a list of `~.patches.Patch` instances rather than the
1586+
default `~.collections.PatchCollection` created by `~.axes.Axes.pcolor`,
1587+
because the latter does not allow the hatch pattern to vary among the
16081588
members of the collection.
16091589
"""
1590+
16101591
def __init__(self, ax, mappable, **kw):
16111592
# we do not want to override the behaviour of solids
16121593
# so add a new attribute which will be a list of the
@@ -1616,7 +1597,7 @@ def __init__(self, ax, mappable, **kw):
16161597

16171598
def _add_solids(self, X, Y, C):
16181599
"""
1619-
Draw the colors using :class:`~matplotlib.patches.Patch`;
1600+
Draw the colors using `~matplotlib.patches.Patch`;
16201601
optionally add separators.
16211602
"""
16221603
n_segments = len(C)
@@ -1668,9 +1649,9 @@ def colorbar_factory(cax, mappable, **kwargs):
16681649
Creates a colorbar on the given axes for the given mappable.
16691650
16701651
Typically, for automatic colorbar placement given only a mappable use
1671-
:meth:`~matplotlib.figure.Figure.colorbar`.
1672-
1652+
`~.Figure.colorbar`.
16731653
"""
1654+
16741655
# if the given mappable is a contourset with any hatching, use
16751656
# ColorbarPatch else use Colorbar
16761657
if (isinstance(mappable, contour.ContourSet)

0 commit comments

Comments
 (0)
0