13
13
a function for resizing an axes and adding a second axes
14
14
suitable for a colorbar
15
15
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`.
20
18
'''
19
+
21
20
import copy
22
21
import logging
23
22
98
97
If None, ticks are determined automatically from the
99
98
input.
100
99
*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.
107
103
*drawedges* bool
108
104
Whether to draw lines at color boundaries.
109
105
============ ====================================================
130
126
Add a colorbar to a plot.
131
127
132
128
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::
135
130
136
131
colorbar(**kwargs)
137
132
colorbar(mappable, **kwargs)
180
175
colorbar properties:
181
176
%s
182
177
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.
185
180
186
181
The *shrink* kwarg provides a simple way to scale the colorbar with respect
187
182
to the axes. Note that if *cax* is specified, it determines the size of the
@@ -422,10 +417,6 @@ class ColorbarBase(_ColorbarMappableDummy):
422
417
423
418
label : str
424
419
"""
425
- _slice_dict = {'neither' : slice (0 , None ),
426
- 'both' : slice (1 , - 1 ),
427
- 'min' : slice (1 , None ),
428
- 'max' : slice (0 , - 1 )}
429
420
430
421
n_rasterize = 50 # rasterize solids if number of colors >= n_rasterize
431
422
@@ -452,8 +443,6 @@ def __init__(self, ax, cmap=None,
452
443
cbook ._check_in_list (
453
444
['auto' , 'left' , 'right' , 'top' , 'bottom' ],
454
445
ticklocation = ticklocation )
455
- cbook ._check_in_list (
456
- self ._slice_dict , extend = extend )
457
446
cbook ._check_in_list (
458
447
['uniform' , 'proportional' ], spacing = spacing )
459
448
@@ -469,7 +458,10 @@ def __init__(self, ax, cmap=None,
469
458
self .values = values
470
459
self .boundaries = boundaries
471
460
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 )
473
465
self .spacing = spacing
474
466
self .orientation = orientation
475
467
self .drawedges = drawedges
@@ -610,11 +602,9 @@ def _use_auto_colorbar_locator(self):
610
602
Return if we should use an adjustable tick locator or a fixed
611
603
one. (check is used twice so factored out here...)
612
604
"""
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 .boundarie
6D4E
s is not None and self .spacing == 'uniform'
606
+ return (type (self .norm ) in [colors .Normalize , colors .LogNorm ]
607
+ and not contouring )
618
608
619
609
def _reset_locator_formatter_scale (self ):
620
610
"""
@@ -624,7 +614,7 @@ def _reset_locator_formatter_scale(self):
624
614
"""
625
615
self .locator = None
626
616
self .formatter = None
627
- if ( isinstance (self .norm , colors .LogNorm ) ):
617
+ if isinstance (self .norm , colors .LogNorm ):
628
618
# *both* axes are made log so that determining the
629
619
# mid point is easier.
630
620
self .ax .set_xscale ('log' )
@@ -644,8 +634,7 @@ def update_ticks(self):
644
634
locator , formatter = self ._get_ticker_locator_formatter ()
645
635
long_axis = ax .yaxis if self .orientation == 'vertical' else ax .xaxis
646
636
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 )
649
638
long_axis .set_major_locator (locator )
650
639
long_axis .set_major_formatter (formatter )
651
640
else :
@@ -803,7 +792,7 @@ def _edges(self, X, Y):
803
792
804
793
def _add_solids (self , X , Y , C ):
805
794
'''
806
- Draw the colors using :meth:`~matplotlib .axes.Axes.pcolormesh`;
795
+ Draw the colors using `~ .axes.Axes.pcolormesh`;
807
796
optionally add separators.
808
797
'''
809
798
if self .orientation == 'vertical' :
@@ -1175,25 +1164,21 @@ def set_alpha(self, alpha):
1175
1164
1176
1165
def remove (self ):
1177
1166
"""
1178
- Remove this colorbar from the figure
1167
+ Remove this colorbar from the figure.
1179
1168
"""
1180
-
1181
1169
fig = self .ax .figure
1182
1170
fig .delaxes (self .ax )
1183
1171
1184
1172
1185
1173
class Colorbar (ColorbarBase ):
1186
1174
"""
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`.
1195
1177
1178
+ It is not intended to be instantiated directly; instead, use
1179
+ `~.figure.Figure.colorbar` or `~.pyplot.colorbar` to make your colorbar.
1196
1180
"""
1181
+
1197
1182
def __init__ (self , ax , mappable , ** kw ):
1198
1183
# Ensure the given mappable's norm has appropriate vmin and vmax set
1199
1184
# even if mappable.draw has not yet been called.
@@ -1230,15 +1215,13 @@ def on_mappable_changed(self, mappable):
1230
1215
1231
1216
Typically this is automatically registered as an event handler
1232
1217
by :func:`colorbar_factory` and should not be called manually.
1233
-
1234
1218
"""
1235
1219
_log .debug ('colorbar mappable changed' )
1236
1220
self .update_normal (mappable )
1237
1221
1238
1222
def add_lines (self , CS , erase = True ):
1239
1223
'''
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.
1242
1225
1243
1226
Set *erase* to False if these lines should be added to
1244
1227
any pre-existing lines.
@@ -1599,14 +1582,12 @@ def make_axes_gridspec(parent, *, fraction=0.15, shrink=1.0, aspect=20, **kw):
1599
1582
1600
1583
class ColorbarPatch (Colorbar ):
1601
1584
"""
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
1608
1588
members of the collection.
1609
1589
"""
1590
+
1610
1591
def __init__ (self , ax , mappable , ** kw ):
1611
1592
# we do not want to override the behaviour of solids
1612
1593
# so add a new attribute which will be a list of the
@@ -1616,7 +1597,7 @@ def __init__(self, ax, mappable, **kw):
1616
1597
1617
1598
def _add_solids (self , X , Y , C ):
1618
1599
"""
1619
- Draw the colors using :class: `~matplotlib.patches.Patch`;
1600
+ Draw the colors using `~matplotlib.patches.Patch`;
1620
1601
optionally add separators.
1621
1602
"""
1622
1603
n_segments = len (C )
@@ -1668,9 +1649,9 @@ def colorbar_factory(cax, mappable, **kwargs):
1668
1649
Creates a colorbar on the given axes for the given mappable.
1669
1650
1670
1651
Typically, for automatic colorbar placement given only a mappable use
1671
- :meth:`~matplotlib.figure.Figure.colorbar`.
1672
-
1652
+ `~.Figure.colorbar`.
1673
1653
"""
1654
+
1674
1655
# if the given mappable is a contourset with any hatching, use
1675
1656
# ColorbarPatch else use Colorbar
1676
1657
if (isinstance (mappable , contour .ContourSet )
0 commit comments