Skip to content

Commit 49aa235

Browse files
zmoonmathause
andauthored
Clean up and enhance plot method docstrings (#5285)
Co-authored-by: Mathias Hauser <mathause@users.noreply.github.com>
1 parent 9165c26 commit 49aa235

File tree

4 files changed

+203
-157
lines changed

4 files changed

+203
-157
lines changed

doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
"hashable": ":term:`hashable <name>`",
145145
# matplotlib terms
146146
"color-like": ":py:func:`color-like <matplotlib.colors.is_color_like>`",
147-
"matplotlib colormap name": ":doc:matplotlib colormap name <Colormap reference>",
147+
"matplotlib colormap name": ":doc:`matplotlib colormap name <matplotlib:gallery/color/colormap_reference>`",
148148
"matplotlib axes object": ":py:class:`matplotlib axes object <matplotlib.axes.Axes>`",
149149
"colormap": ":py:class:`colormap <matplotlib.colors.Colormap>`",
150150
# objects without namespace

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ Bug fixes
5252

5353
Documentation
5454
~~~~~~~~~~~~~
55+
- Clean up and enhance docstrings for the :py:class:`DataArray.plot` and ``Dataset.plot.*``
56+
families of methods (:pull:`5285`).
57+
By `Zach Moon <https://github.com/zmoon>`_.
5558

5659
- Explanation of deprecation cycles and how to implement them added to contributors
5760
guide. (:pull:`5289`)

xarray/plot/dataset_plot.py

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -196,82 +196,97 @@ def _dsplot(plotfunc):
196196
197197
ds : Dataset
198198
x, y : str
199-
Variable names for x, y axis.
199+
Variable names for the *x* and *y* grid positions.
200200
u, v : str, optional
201-
Variable names for quiver or streamplot plots only
201+
Variable names for the *u* and *v* velocities
202+
(in *x* and *y* direction, respectively; quiver/streamplot plots only).
202203
hue: str, optional
203-
Variable by which to color scattered points or arrows
204-
hue_style: str, optional
205-
Can be either 'discrete' (legend) or 'continuous' (color bar).
204+
Variable by which to color scatter points or arrows.
205+
hue_style: {'continuous', 'discrete'}, optional
206+
How to use the ``hue`` variable:
207+
208+
- ``'continuous'`` -- continuous color scale
209+
(default for numeric ``hue`` variables)
210+
- ``'discrete'`` -- a color for each unique value, using the default color cycle
211+
(default for non-numeric ``hue`` variables)
206212
markersize: str, optional
207-
scatter only. Variable by which to vary size of scattered points.
208-
size_norm: optional
209-
Either None or 'Norm' instance to normalize the 'markersize' variable.
213+
Variable by which to vary the size of scattered points (scatter plot only).
214+
size_norm: matplotlib.colors.Normalize or tuple, optional
215+
Used to normalize the ``markersize`` variable.
216+
If a tuple is passed, the values will be passed to
217+
:py:class:`matplotlib:matplotlib.colors.Normalize` as arguments.
218+
Default: no normalization (``vmin=None``, ``vmax=None``, ``clip=False``).
210219
scale: scalar, optional
211220
Quiver only. Number of data units per arrow length unit.
212221
Use this to control the length of the arrows: larger values lead to
213-
smaller arrows
214-
add_guide: bool, optional
215-
Add a guide that depends on hue_style
216-
- for "discrete", build a legend.
217-
This is the default for non-numeric `hue` variables.
218-
- for "continuous", build a colorbar
222+
smaller arrows.
223+
add_guide: bool, optional, default: True
224+
Add a guide that depends on ``hue_style``:
225+
226+
- ``'continuous'`` -- build a colorbar
227+
- ``'discrete'`` -- build a legend
219228
row : str, optional
220-
If passed, make row faceted plots on this dimension name
229+
If passed, make row faceted plots on this dimension name.
221230
col : str, optional
222-
If passed, make column faceted plots on this dimension name
231+
If passed, make column faceted plots on this dimension name.
223232
col_wrap : int, optional
224-
Use together with ``col`` to wrap faceted plots
233+
Use together with ``col`` to wrap faceted plots.
225234
ax : matplotlib axes object, optional
226-
If None, uses the current axis. Not applicable when using facets.
235+
If ``None``, use the current axes. Not applicable when using facets.
227236
subplot_kws : dict, optional
228-
Dictionary of keyword arguments for matplotlib subplots. Only applies
229-
to FacetGrid plotting.
237+
Dictionary of keyword arguments for Matplotlib subplots
238+
(see :py:meth:`matplotlib:matplotlib.figure.Figure.add_subplot`).
239+
Only applies to FacetGrid plotting.
230240
aspect : scalar, optional
231-
Aspect ratio of plot, so that ``aspect * size`` gives the width in
241+
Aspect ratio of plot, so that ``aspect * size`` gives the *width* in
232242
inches. Only used if a ``size`` is provided.
233243
size : scalar, optional
234-
If provided, create a new figure for the plot with the given size.
235-
Height (in inches) of each plot. See also: ``aspect``.
236-
norm : ``matplotlib.colors.Normalize`` instance, optional
237-
If the ``norm`` has vmin or vmax specified, the corresponding kwarg
238-
must be None.
244+
If provided, create a new figure for the plot with the given size:
245+
*height* (in inches) of each plot. See also: ``aspect``.
246+
norm : matplotlib.colors.Normalize, optional
247+
If ``norm`` has ``vmin`` or ``vmax`` specified, the corresponding
248+
kwarg must be ``None``.
239249
vmin, vmax : float, optional
240250
Values to anchor the colormap, otherwise they are inferred from the
241251
data and other keyword arguments. When a diverging dataset is inferred,
242252
setting one of these values will fix the other by symmetry around
243253
``center``. Setting both values prevents use of a diverging colormap.
244254
If discrete levels are provided as an explicit list, both of these
245255
values are ignored.
246-
cmap : str or colormap, optional
256+
cmap : matplotlib colormap name or colormap, optional
247257
The mapping from data values to color space. Either a
248-
matplotlib colormap name or object. If not provided, this will
249-
be either ``viridis`` (if the function infers a sequential
250-
dataset) or ``RdBu_r`` (if the function infers a diverging
251-
dataset). When `Seaborn` is installed, ``cmap`` may also be a
252-
`seaborn` color palette. If ``cmap`` is seaborn color palette,
258+
Matplotlib colormap name or object. If not provided, this will
259+
be either ``'viridis'`` (if the function infers a sequential
260+
dataset) or ``'RdBu_r'`` (if the function infers a diverging
261+
dataset).
262+
See :doc:`Choosing Colormaps in Matplotlib <matplotlib:tutorials/colors/colormaps>`
263+
for more information.
264+
265+
If *seaborn* is installed, ``cmap`` may also be a
266+
`seaborn color palette <https://seaborn.pydata.org/tutorial/color_palettes.html>`_.
267+
Note: if ``cmap`` is a seaborn color palette,
253268
``levels`` must also be specified.
254-
colors : color-like or list of color-like, optional
269+
colors : str or array-like of color-like, optional
255270
A single color or a list of colors. The ``levels`` argument
256271
is required.
257272
center : float, optional
258273
The value at which to center the colormap. Passing this value implies
259274
use of a diverging colormap. Setting it to ``False`` prevents use of a
260275
diverging colormap.
261276
robust : bool, optional
262-
If True and ``vmin`` or ``vmax`` are absent, the colormap range is
277+
If ``True`` and ``vmin`` or ``vmax`` are absent, the colormap range is
263278
computed with 2nd and 98th percentiles instead of the extreme values.
264-
extend : {"neither", "both", "min", "max"}, optional
279+
extend : {'neither', 'both', 'min', 'max'}, optional
265280
How to draw arrows extending the colorbar beyond its limits. If not
266-
provided, extend is inferred from vmin, vmax and the data limits.
267-
levels : int or list-like object, optional
268-
Split the colormap (cmap) into discrete color intervals. If an integer
281+
provided, ``extend`` is inferred from ``vmin``, ``vmax`` and the data limits.
282+
levels : int or array-like, optional
283+
Split the colormap (``cmap``) into discrete color intervals. If an integer
269284
is provided, "nice" levels are chosen based on the data range: this can
270285
imply that the final number of levels is not exactly the expected one.
271286
Setting ``vmin`` and/or ``vmax`` with ``levels=N`` is equivalent to
272287
setting ``levels=np.linspace(vmin, vmax, N)``.
273288
**kwargs : optional
274-
Additional keyword arguments to matplotlib
289+
Additional keyword arguments to wrapped Matplotlib function.
275290
"""
276291

277292
# Build on the original docstring
@@ -463,9 +478,11 @@ def plotmethod(
463478

464479

465480
@_dsplot
466-
def scatter(ds, x, y, ax, u, v, **kwargs):
481+
def scatter(ds, x, y, ax, **kwargs):
467482
"""
468483
Scatter Dataset data variables against each other.
484+
485+
Wraps :py:func:`matplotlib:matplotlib.pyplot.scatter`.
469486
"""
470487

471488
if "add_colorbar" in kwargs or "add_legend" in kwargs:
@@ -482,6 +499,10 @@ def scatter(ds, x, y, ax, u, v, **kwargs):
482499
size_norm = kwargs.pop("size_norm", None)
483500
size_mapping = kwargs.pop("size_mapping", None) # set by facetgrid
484501

502+
# Remove `u` and `v` so they don't get passed to `ax.scatter`
503+
kwargs.pop("u", None)
504+
kwargs.pop("v", None)
505+
485506
# need to infer size_mapping with full dataset
486507
data = _infer_scatter_data(ds, x, y, hue, markersize, size_norm, size_mapping)
487508

@@ -519,7 +540,10 @@ def scatter(ds, x, y, ax, u, v, **kwargs):
519540

520541
@_dsplot
521542
def quiver(ds, x, y, ax, u, v, **kwargs):
522-
"""Quiver plot with Dataset variables."""
543+
"""Quiver plot of Dataset variables.
544+
545+
Wraps :py:func:`matplotlib:matplotlib.pyplot.quiver`.
546+
"""
523547
import matplotlib as mpl
524548

525549
if x is None or y is None or u is None or v is None:
@@ -548,7 +572,10 @@ def quiver(ds, x, y, ax, u, v, **kwargs):
548572

549573
@_dsplot
550574
def streamplot(ds, x, y, ax, u, v, **kwargs):
551-
"""Quiver plot with Dataset variables."""
575+
"""Plot streamlines of Dataset variables.
576+
577+
Wraps :py:func:`matplotlib:matplotlib.pyplot.streamplot`.
578+
"""
552579
import matplotlib as mpl
553580

554581
if x is None or y is None or u is None or v is None:

0 commit comments

Comments
 (0)