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

Skip to content

Commit 0015687

Browse files
committed
Move colorbar() doc to method itself.
It seems clearer to just put in the docstring "as is" at the (only) expected place rather than define it in another module and move it around. Also group all "other parameters" together into a standard numpydoc section, which avoids having to fiddle with indents.
1 parent bf9a451 commit 0015687

File tree

2 files changed

+79
-83
lines changed

2 files changed

+79
-83
lines changed

lib/matplotlib/colorbar.py

Lines changed: 11 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"""
1313

1414
import logging
15-
import textwrap
1615

1716
import numpy as np
1817

@@ -28,7 +27,8 @@
2827

2928
_log = logging.getLogger(__name__)
3029

31-
_make_axes_kw_doc = """
30+
_docstring.interpd.update(
31+
_make_axes_kw_doc="""
3232
location : None or {'left', 'right', 'top', 'bottom'}
3333
The location, relative to the parent axes, where the colorbar axes
3434
is created. It also determines the *orientation* of the colorbar
@@ -61,10 +61,8 @@
6161
panchor : (float, float), or *False*, optional
6262
The anchor point of the colorbar parent axes. If *False*, the parent
6363
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="""
6866
extend : {'neither', 'both', 'min', 'max'}
6967
Make pointed end(s) for out-of-range values (unless 'neither'). These are
7068
set for a given colormap using the colormap set_under and set_over methods.
@@ -114,76 +112,7 @@
114112
each region delimited by adjacent entries in *boundaries*, the color mapped
115113
to the corresponding value in values will be used.
116114
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.""")
187116

188117

189118
def _set_ticks_on_axis_warn(*args, **kwargs):
@@ -267,7 +196,7 @@ def get_subplotspec(self):
267196
return ss()
268197

269198

270-
@_docstring.Substitution(_colormap_kw_doc)
199+
@_docstring.interpd
271200
class Colorbar:
272201
r"""
273202
Draw a colorbar in an existing axes.
@@ -327,7 +256,7 @@ class Colorbar:
327256
drawedges : bool
328257
329258
filled : bool
330-
%s
259+
%(_colormap_kw_doc)s
331260
"""
332261

333262
n_rasterize = 50 # rasterize solids if number of colors >= n_rasterize
@@ -1404,7 +1333,7 @@ def _normalize_location_orientation(location, orientation):
14041333
return loc_settings
14051334

14061335

1407-
@_docstring.Substitution(_make_axes_kw_doc)
1336+
@_docstring.interpd
14081337
def make_axes(parents, location=None, orientation=None, fraction=0.15,
14091338
shrink=1.0, aspect=20, **kwargs):
14101339
"""
@@ -1417,7 +1346,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
14171346
----------
14181347
parents : `~.axes.Axes` or list of `~.axes.Axes`
14191348
The Axes to use as parents for placing the colorbar.
1420-
%s
1349+
%(_make_axes_kw_doc)s
14211350
14221351
Returns
14231352
-------
@@ -1506,7 +1435,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
15061435
return cax, kwargs
15071436

15081437

1509-
@_docstring.Substitution(_make_axes_kw_doc)
1438+
@_docstring.interpd
15101439
def make_axes_gridspec(parent, *, location=None, orientation=None,
15111440
fraction=0.15, shrink=1.0, aspect=20, **kwargs):
15121441
"""
@@ -1532,7 +1461,7 @@ def make_axes_gridspec(parent, *, location=None, orientation=None,
15321461
----------
15331462
parent : `~.axes.Axes`
15341463
The Axes to use as parent for placing the colorbar.
1535-
%s
1464+
%(_make_axes_kw_doc)s
15361465
15371466
Returns
15381467
-------

lib/matplotlib/figure.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,74 @@ def text(self, x, y, s, fontdict=None, **kwargs):
11831183
@_docstring.dedent_interpd
11841184
def colorbar(
11851185
self, mappable, cax=None, ax=None, use_gridspec=True, **kwargs):
1186-
"""%(colorbar_doc)s"""
1186+
"""
1187+
Add a colorbar to a plot.
1188+
1189+
Parameters
1190+
----------
1191+
mappable
1192+
The `matplotlib.cm.ScalarMappable` (i.e., `.AxesImage`,
1193+
`.ContourSet`, etc.) described by this colorbar. This argument is
1194+
mandatory for the `.Figure.colorbar` method but optional for the
1195+
`.pyplot.colorbar` function, which sets the default to the current
1196+
image.
1197+
1198+
Note that one can create a `.ScalarMappable` "on-the-fly" to
1199+
generate colorbars not attached to a previously drawn artist, e.g.
1200+
::
1201+
1202+
fig.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap), ax=ax)
1203+
1204+
cax : `~matplotlib.axes.Axes`, optional
1205+
Axes into which the colorbar will be drawn.
1206+
1207+
ax : `~matplotlib.axes.Axes`, list of Axes, optional
1208+
One or more parent axes from which space for a new colorbar axes
1209+
will be stolen, if *cax* is None. This has no effect if *cax* is
1210+
set.
1211+
1212+
use_gridspec : bool, optional
1213+
If *cax* is ``None``, a new *cax* is created as an instance of
1214+
Axes. If *ax* is an instance of Subplot and *use_gridspec* is
1215+
``True``, *cax* is created as an instance of Subplot using the
1216+
:mod:`.gridspec` module.
1217+
1218+
Returns
1219+
-------
1220+
colorbar : `~matplotlib.colorbar.Colorbar`
1221+
1222+
Other Parameters
1223+
----------------
1224+
%(_make_axes_kw_doc)s
1225+
%(_colormap_kw_doc)s
1226+
1227+
Notes
1228+
-----
1229+
If *mappable* is a `~.contour.ContourSet`, its *extend* kwarg is
1230+
included automatically.
1231+
1232+
The *shrink* kwarg provides a simple way to scale the colorbar with
1233+
respect to the axes. Note that if *cax* is specified, it determines the
1234+
size of the colorbar and *shrink* and *aspect* kwargs are ignored.
1235+
1236+
For more precise control, you can manually specify the positions of the
1237+
axes objects in which the mappable and the colorbar are drawn. In this
1238+
case, do not use any of the axes properties kwargs.
1239+
1240+
It is known that some vector graphics viewers (svg and pdf) renders
1241+
white gaps between segments of the colorbar. This is due to bugs in
1242+
the viewers, not Matplotlib. As a workaround, the colorbar can be
1243+
rendered with overlapping segments::
1244+
1245+
cbar = colorbar()
1246+
cbar.solids.set_edgecolor("face")
1247+
draw()
1248+
1249+
However this has negative consequences in other circumstances, e.g.
1250+
with semi-transparent images (alpha < 1) and colorbar extensions;
1251+
therefore, this workaround is not used by default (see issue #1188).
1252+
"""
1253+
11871254
if ax is None:
11881255
ax = getattr(mappable, "axes", self.gca())
11891256

0 commit comments

Comments
 (0)