12
12
"""
13
13
14
14
import logging
15
- import textwrap
16
15
17
16
import numpy as np
18
17
28
27
29
28
_log = logging .getLogger (__name__ )
30
29
31
- _make_axes_kw_doc = """
30
+ _docstring .interpd .update (
31
+ _make_axes_kw_doc = """
32
32
location : None or {'left', 'right', 'top', 'bottom'}
33
33
The location, relative to the parent axes, where the colorbar axes
34
34
is created. It also determines the *orientation* of the colorbar
61
61
panchor : (float, float), or *False*, optional
62
62
The anchor point of the colorbar parent axes. If *False*, the parent
63
63
axes' anchor will be unchanged.
64
- Defaults to (1.0, 0.5) if vertical; (0.5, 0.0) if horizontal.
65
- """
66
-
67
- _colormap_kw_doc = """
64
+ Defaults to (1.0, 0.5) if vertical; (0.5, 0.0) if horizontal.""" ,
65
+ _colormap_kw_doc = """
68
66
extend : {'neither', 'both', 'min', 'max'}
69
67
Make pointed end(s) for out-of-range values (unless 'neither'). These are
70
68
set for a given colormap using the colormap set_under and set_over methods.
114
112
each region delimited by adjacent entries in *boundaries*, the color mapped
115
113
to the corresponding value in values will be used.
116
114
Normally only useful for indexed colors (i.e. ``norm=NoNorm()``) or other
117
- unusual circumstances.
118
- """
119
-
120
- _docstring .interpd .update (colorbar_doc = """
121
- Add a colorbar to a plot.
122
-
123
- Parameters
124
- ----------
125
- mappable
126
- The `matplotlib.cm.ScalarMappable` (i.e., `~matplotlib.image.AxesImage`,
127
- `~matplotlib.contour.ContourSet`, etc.) described by this colorbar.
128
- This argument is mandatory for the `.Figure.colorbar` method but optional
129
- for the `.pyplot.colorbar` function, which sets the default to the current
130
- image.
131
-
132
- Note that one can create a `.ScalarMappable` "on-the-fly" to generate
133
- colorbars not attached to a previously drawn artist, e.g. ::
134
-
135
- fig.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap), ax=ax)
136
-
137
- cax : `~matplotlib.axes.Axes`, optional
138
- Axes into which the colorbar will be drawn.
139
-
140
- ax : `~matplotlib.axes.Axes`, list of Axes, optional
141
- One or more parent axes from which space for a new colorbar axes will be
142
- stolen, if *cax* is None. This has no effect if *cax* is set.
143
-
144
- use_gridspec : bool, optional
145
- If *cax* is ``None``, a new *cax* is created as an instance of Axes. If
146
- *ax* is an instance of Subplot and *use_gridspec* is ``True``, *cax* is
147
- created as an instance of Subplot using the :mod:`.gridspec` module.
148
-
149
- Returns
150
- -------
151
- colorbar : `~matplotlib.colorbar.Colorbar`
152
-
153
- Notes
154
- -----
155
- Additional keyword arguments are of two kinds:
156
-
157
- axes properties:
158
- %s
159
- colorbar properties:
160
- %s
161
-
162
- If *mappable* is a `~.contour.ContourSet`, its *extend* kwarg is included
163
- automatically.
164
-
165
- The *shrink* kwarg provides a simple way to scale the colorbar with respect
166
- to the axes. Note that if *cax* is specified, it determines the size of the
167
- colorbar and *shrink* and *aspect* kwargs are ignored.
168
-
169
- For more precise control, you can manually specify the positions of
170
- the axes objects in which the mappable and the colorbar are drawn. In
171
- this case, do not use any of the axes properties kwargs.
172
-
173
- It is known that some vector graphics viewers (svg and pdf) renders white gaps
174
- between segments of the colorbar. This is due to bugs in the viewers, not
175
- Matplotlib. As a workaround, the colorbar can be rendered with overlapping
176
- segments::
177
-
178
- cbar = colorbar()
179
- cbar.solids.set_edgecolor("face")
180
- draw()
181
-
182
- However this has negative consequences in other circumstances, e.g. with
183
- semi-transparent images (alpha < 1) and colorbar extensions; therefore, this
184
- workaround is not used by default (see issue #1188).
185
- """ % (textwrap .indent (_make_axes_kw_doc , " " ),
186
- textwrap .indent (_colormap_kw_doc , " " )))
115
+ unusual circumstances.""" )
187
116
188
117
189
118
def _set_ticks_on_axis_warn (* args , ** kwargs ):
@@ -267,7 +196,7 @@ def get_subplotspec(self):
267
196
return ss ()
268
197
269
198
270
- @_docstring .Substitution ( _colormap_kw_doc )
199
+ @_docstring .interpd
271
200
class Colorbar :
272
201
r"""
273
202
Draw a colorbar in an existing axes.
@@ -327,7 +256,7 @@ class Colorbar:
327
256
drawedges : bool
328
257
329
258
filled : bool
330
- %s
259
+ %(_colormap_kw_doc) s
331
260
"""
332
261
333
262
n_rasterize = 50 # rasterize solids if number of colors >= n_rasterize
@@ -434,7 +363,8 @@ def __init__(self, ax, mappable=None, *, cmap=None,
434
363
self .dividers = collections .LineCollection (
435
364
[],
436
365
colors = [mpl .rcParams ['axes.edgecolor' ]],
437
- linewidths = [0.5 * mpl .rcParams ['axes.linewidth' ]])
366
+ linewidths = [0.5 * mpl .rcParams ['axes.linewidth' ]],
367
+ clip_on = False )
438
368
self .ax .add_collection (self .dividers )
439
369
440
370
self ._locator = None
@@ -650,12 +580,31 @@ def _add_solids(self, X, Y, C):
650
580
if not self .drawedges :
651
581
if len (self ._y ) >= self .n_rasterize :
652
582
self .solids .set_rasterized (True )
653
- if self .drawedges :
654
- start_idx = 0 if self ._extend_lower () else 1
655
- end_idx = len (X ) if self ._extend_upper () else - 1
656
- self .dividers .set_segments (np .dstack ([X , Y ])[start_idx :end_idx ])
657
- else :
583
+ self ._update_dividers ()
584
+
585
+ def _update_dividers (self ):
586
+ if not self .drawedges :
658
587
self .dividers .set_segments ([])
588
+ return
589
+ # Place all *internal* dividers.
590
+ if self .orientation == 'vertical' :
591
+ lims = self .ax .get_ylim ()
592
+ bounds = (lims [0 ] < self ._y ) & (self ._y < lims [1 ])
593
+ else :
594
+ lims = self .ax .get_xlim ()
595
+ bounds = (lims [0 ] < self ._y ) & (self ._y < lims [1 ])
596
+ y = self ._y [bounds ]
597
+ # And then add outer dividers if extensions are on.
598
+ if self ._extend_lower ():
599
+ y = np .insert (y , 0 , lims [0 ])
600
+ if self ._extend_upper ():
601
+ y = np .append (y , lims [1 ])
602
+ X , Y = np .meshgrid ([0 , 1 ], y )
603
+ if self .orientation == 'vertical' :
604
+ segments = np .dstack ([X , Y ])
605
+ else :
606
+ segments = np .dstack ([Y , X ])
607
+ self .dividers .set_segments (segments )
659
608
660
609
def _add_solids_patches (self , X , Y , C , mappable ):
661
610
hatches = mappable .hatches * len (C ) # Have enough hatches.
@@ -760,7 +709,8 @@ def _do_extends(self, ax=None):
760
709
zorder = np .nextafter (self .ax .patch .zorder , - np .inf ))
761
710
self .ax .add_patch (patch )
762
711
self ._extend_patches .append (patch )
763
- return
712
+
713
+ self ._update_dividers ()
764
714
765
715
def add_lines (self , * args , ** kwargs ):
766
716
"""
@@ -1404,7 +1354,7 @@ def _normalize_location_orientation(location, orientation):
1404
1354
return loc_settings
1405
1355
1406
1356
1407
- @_docstring .Substitution ( _make_axes_kw_doc )
1357
+ @_docstring .interpd
1408
1358
def make_axes (parents , location = None , orientation = None , fraction = 0.15 ,
1409
1359
shrink = 1.0 , aspect = 20 , ** kwargs ):
1410
1360
"""
@@ -1417,7 +1367,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
1417
1367
----------
1418
1368
parents : `~.axes.Axes` or list of `~.axes.Axes`
1419
1369
The Axes to use as parents for placing the colorbar.
1420
- %s
1370
+ %(_make_axes_kw_doc) s
1421
1371
1422
1372
Returns
1423
1373
-------
@@ -1506,7 +1456,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
1506
1456
return cax , kwargs
1507
1457
1508
1458
1509
- @_docstring .Substitution ( _make_axes_kw_doc )
1459
+ @_docstring .interpd
1510
1460
def make_axes_gridspec (parent , * , location = None , orientation = None ,
1511
1461
fraction = 0.15 , shrink = 1.0 , aspect = 20 , ** kwargs ):
1512
1462
"""
@@ -1532,7 +1482,7 @@ def make_axes_gridspec(parent, *, location=None, orientation=None,
1532
1482
----------
1533
1483
parent : `~.axes.Axes`
1534
1484
The Axes to use as parent for placing the colorbar.
1535
- %s
1485
+ %(_make_axes_kw_doc) s
1536
1486
1537
1487
Returns
1538
1488
-------
0 commit comments