Skip to content

Commit 988af43

Browse files
committed
Merge branch 'main' into listbackends
* main: Add import ASV benchmark (pydata#7176) Rework docs about scatter plots (pydata#7169) Fix some scatter plot issues (pydata#7167) Fix doctest warnings, enable errors in CI (pydata#7166) fix broken test (pydata#7168) Add typing to plot methods (pydata#7052) Fix warning in doctest (pydata#7165)
2 parents 3b7fece + 15c6836 commit 988af43

25 files changed

+5176
-2307
lines changed

.github/workflows/ci-additional.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ jobs:
7272
python xarray/util/print_versions.py
7373
- name: Run doctests
7474
run: |
75-
python -m pytest --doctest-modules xarray --ignore xarray/tests
75+
# Raise an error if there are warnings in the doctests, with `-Werror`.
76+
# This is a trial; if it presents an problem, feel free to remove.
77+
# See https://github.com/pydata/xarray/issues/7164 for more info.
78+
python -m pytest --doctest-modules xarray --ignore xarray/tests -Werror
7679
7780
mypy:
7881
name: Mypy

asv_bench/benchmarks/import.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Import:
2+
"""Benchmark importing xarray"""
3+
4+
def timeraw_import_xarray(self):
5+
return """
6+
import xarray
7+
"""
8+
9+
def timeraw_import_xarray_plot(self):
10+
return """
11+
import xarray.plot
12+
"""
13+
14+
def timeraw_import_xarray_backends(self):
15+
return """
16+
from xarray.backends import list_engines
17+
list_engines()
18+
"""

ci/requirements/min-all-deps.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ channels:
33
- conda-forge
44
- nodefaults
55
dependencies:
6-
# MINIMUM VERSIONS POLICY: see doc/installing.rst
6+
# MINIMUM VERSIONS POLICY: see doc/user-guide/installing.rst
77
# Run ci/min_deps_check.py to verify that this file respects the policy.
88
# When upgrading python, numpy, or pandas, must also change
9-
# doc/installing.rst and setup.py.
9+
# doc/user-guide/installing.rst, doc/user-guide/plotting.rst and setup.py.
1010
- python=3.8
1111
- boto3=1.18
1212
- bottleneck=1.3

doc/api-hidden.rst

-5
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,6 @@
330330
plot.scatter
331331
plot.surface
332332

333-
plot.FacetGrid.map_dataarray
334-
plot.FacetGrid.set_titles
335-
plot.FacetGrid.set_ticks
336-
plot.FacetGrid.map
337-
338333
CFTimeIndex.all
339334
CFTimeIndex.any
340335
CFTimeIndex.append

doc/api.rst

+2
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ DataArray
703703
DataArray.plot.line
704704
DataArray.plot.pcolormesh
705705
DataArray.plot.step
706+
DataArray.plot.scatter
706707
DataArray.plot.surface
707708

708709

@@ -719,6 +720,7 @@ Faceting
719720
plot.FacetGrid.map_dataarray
720721
plot.FacetGrid.map_dataarray_line
721722
plot.FacetGrid.map_dataset
723+
plot.FacetGrid.map_plot1d
722724
plot.FacetGrid.set_axis_labels
723725
plot.FacetGrid.set_ticks
724726
plot.FacetGrid.set_titles

doc/user-guide/plotting.rst

+79-26
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Matplotlib must be installed before xarray can plot.
2727

2828
To use xarray's plotting capabilities with time coordinates containing
2929
``cftime.datetime`` objects
30-
`nc-time-axis <https://github.com/SciTools/nc-time-axis>`_ v1.2.0 or later
30+
`nc-time-axis <https://github.com/SciTools/nc-time-axis>`_ v1.3.0 or later
3131
needs to be installed.
3232

3333
For more extensive plotting applications consider the following projects:
@@ -106,7 +106,13 @@ The simplest way to make a plot is to call the :py:func:`DataArray.plot()` metho
106106
@savefig plotting_1d_simple.png width=4in
107107
air1d.plot()
108108
109-
Xarray uses the coordinate name along with metadata ``attrs.long_name``, ``attrs.standard_name``, ``DataArray.name`` and ``attrs.units`` (if available) to label the axes. The names ``long_name``, ``standard_name`` and ``units`` are copied from the `CF-conventions spec <https://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/build/ch03s03.html>`_. When choosing names, the order of precedence is ``long_name``, ``standard_name`` and finally ``DataArray.name``. The y-axis label in the above plot was constructed from the ``long_name`` and ``units`` attributes of ``air1d``.
109+
Xarray uses the coordinate name along with metadata ``attrs.long_name``,
110+
``attrs.standard_name``, ``DataArray.name`` and ``attrs.units`` (if available)
111+
to label the axes.
112+
The names ``long_name``, ``standard_name`` and ``units`` are copied from the
113+
`CF-conventions spec <https://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/build/ch03s03.html>`_.
114+
When choosing names, the order of precedence is ``long_name``, ``standard_name`` and finally ``DataArray.name``.
115+
The y-axis label in the above plot was constructed from the ``long_name`` and ``units`` attributes of ``air1d``.
110116

111117
.. ipython:: python
112118
@@ -340,7 +346,10 @@ The keyword arguments ``xincrease`` and ``yincrease`` let you control the axes d
340346
y="lat", hue="lon", xincrease=False, yincrease=False
341347
)
342348
343-
In addition, one can use ``xscale, yscale`` to set axes scaling; ``xticks, yticks`` to set axes ticks and ``xlim, ylim`` to set axes limits. These accept the same values as the matplotlib methods ``Axes.set_(x,y)scale()``, ``Axes.set_(x,y)ticks()``, ``Axes.set_(x,y)lim()`` respectively.
349+
In addition, one can use ``xscale, yscale`` to set axes scaling;
350+
``xticks, yticks`` to set axes ticks and ``xlim, ylim`` to set axes limits.
351+
These accept the same values as the matplotlib methods ``Axes.set_(x,y)scale()``,
352+
``Axes.set_(x,y)ticks()``, ``Axes.set_(x,y)lim()`` respectively.
344353

345354

346355
Two Dimensions
@@ -350,7 +359,8 @@ Two Dimensions
350359
Simple Example
351360
================
352361

353-
The default method :py:meth:`DataArray.plot` calls :py:func:`xarray.plot.pcolormesh` by default when the data is two-dimensional.
362+
The default method :py:meth:`DataArray.plot` calls :py:func:`xarray.plot.pcolormesh`
363+
by default when the data is two-dimensional.
354364

355365
.. ipython:: python
356366
:okwarning:
@@ -585,7 +595,10 @@ Faceting here refers to splitting an array along one or two dimensions and
585595
plotting each group.
586596
Xarray's basic plotting is useful for plotting two dimensional arrays. What
587597
about three or four dimensional arrays? That's where facets become helpful.
588-
The general approach to plotting here is called “small multiples”, where the same kind of plot is repeated multiple times, and the specific use of small multiples to display the same relationship conditioned on one or more other variables is often called a “trellis plot”.
598+
The general approach to plotting here is called “small multiples”, where the
599+
same kind of plot is repeated multiple times, and the specific use of small
600+
multiples to display the same relationship conditioned on one or more other
601+
variables is often called a “trellis plot”.
589602

590603
Consider the temperature data set. There are 4 observations per day for two
591604
years which makes for 2920 values along the time dimension.
@@ -670,8 +683,8 @@ Faceted plotting supports other arguments common to xarray 2d plots.
670683
671684
@savefig plot_facet_robust.png
672685
g = hasoutliers.plot.pcolormesh(
673-
"lon",
674-
"lat",
686+
x="lon",
687+
y="lat",
675688
col="time",
676689
col_wrap=3,
677690
robust=True,
@@ -711,7 +724,7 @@ they have been plotted.
711724
.. ipython:: python
712725
:okwarning:
713726
714-
g = t.plot.imshow("lon", "lat", col="time", col_wrap=3, robust=True)
727+
g = t.plot.imshow(x="lon", y="lat", col="time", col_wrap=3, robust=True)
715728
716729
for i, ax in enumerate(g.axes.flat):
717730
ax.set_title("Air Temperature %d" % i)
@@ -727,7 +740,8 @@ they have been plotted.
727740
axis labels, axis ticks and plot titles. See :py:meth:`~xarray.plot.FacetGrid.set_titles`,
728741
:py:meth:`~xarray.plot.FacetGrid.set_xlabels`, :py:meth:`~xarray.plot.FacetGrid.set_ylabels` and
729742
:py:meth:`~xarray.plot.FacetGrid.set_ticks` for more information.
730-
Plotting functions can be applied to each subset of the data by calling :py:meth:`~xarray.plot.FacetGrid.map_dataarray` or to each subplot by calling :py:meth:`~xarray.plot.FacetGrid.map`.
743+
Plotting functions can be applied to each subset of the data by calling
744+
:py:meth:`~xarray.plot.FacetGrid.map_dataarray` or to each subplot by calling :py:meth:`~xarray.plot.FacetGrid.map`.
731745

732746
TODO: add an example of using the ``map`` method to plot dataset variables
733747
(e.g., with ``plt.quiver``).
@@ -742,14 +756,32 @@ Consider this dataset
742756

743757
.. ipython:: python
744758
745-
ds = xr.tutorial.scatter_example_dataset()
759+
ds = xr.tutorial.scatter_example_dataset(seed=42)
746760
ds
747761
748762
749763
Scatter
750764
~~~~~~~
751765

752-
Suppose we want to scatter ``A`` against ``B``
766+
Let's plot the ``A`` DataArray as a function of the ``y`` coord
767+
768+
.. ipython:: python
769+
:okwarning:
770+
771+
ds.A
772+
773+
@savefig da_A_y.png
774+
ds.A.plot.scatter(x="y")
775+
776+
Same plot can be displayed using the dataset:
777+
778+
.. ipython:: python
779+
:okwarning:
780+
781+
@savefig ds_A_y.png
782+
ds.plot.scatter(x="y", y="A")
783+
784+
Now suppose we want to scatter the ``A`` DataArray against the ``B`` DataArray
753785

754786
.. ipython:: python
755787
:okwarning:
@@ -765,36 +797,55 @@ The ``hue`` kwarg lets you vary the color by variable value
765797
@savefig ds_hue_scatter.png
766798
ds.plot.scatter(x="A", y="B", hue="w")
767799
768-
When ``hue`` is specified, a colorbar is added for numeric ``hue`` DataArrays by
769-
default and a legend is added for non-numeric ``hue`` DataArrays (as above).
770-
You can force a legend instead of a colorbar by setting ``hue_style='discrete'``.
771-
Additionally, the boolean kwarg ``add_guide`` can be used to prevent the display of a legend or colorbar (as appropriate).
800+
You can force a legend instead of a colorbar by setting ``add_legend=True, add_colorbar=False``.
772801

773802
.. ipython:: python
774803
:okwarning:
775804
776-
ds = ds.assign(w=[1, 2, 3, 5])
777805
@savefig ds_discrete_legend_hue_scatter.png
778-
ds.plot.scatter(x="A", y="B", hue="w", hue_style="discrete")
806+
ds.plot.scatter(x="A", y="B", hue="w", add_legend=True, add_colorbar=False)
807+
808+
.. ipython:: python
809+
:okwarning:
810+
811+
@savefig ds_discrete_colorbar_hue_scatter.png
812+
ds.plot.scatter(x="A", y="B", hue="w", add_legend=False, add_colorbar=True)
779813
780-
The ``markersize`` kwarg lets you vary the point's size by variable value. You can additionally pass ``size_norm`` to control how the variable's values are mapped to point sizes.
814+
The ``markersize`` kwarg lets you vary the point's size by variable value.
815+
You can additionally pass ``size_norm`` to control how the variable's values are mapped to point sizes.
781816

782817
.. ipython:: python
783818
:okwarning:
784819
785820
@savefig ds_hue_size_scatter.png
786-
ds.plot.scatter(x="A", y="B", hue="z", hue_style="discrete", markersize="z")
821+
ds.plot.scatter(x="A", y="B", hue="y", markersize="z")
822+
823+
The ``z`` kwarg lets you plot the data along the z-axis as well.
824+
825+
.. ipython:: python
826+
:okwarning:
827+
828+
@savefig ds_hue_size_scatter_z.png
829+
ds.plot.scatter(x="A", y="B", z="z", hue="y", markersize="x")
787830
788831
Faceting is also possible
789832

790833
.. ipython:: python
791834
:okwarning:
792835
793836
@savefig ds_facet_scatter.png
794-
ds.plot.scatter(x="A", y="B", col="x", row="z", hue="w", hue_style="discrete")
837+
ds.plot.scatter(x="A", y="B", hue="y", markersize="x", row="x", col="w")
838+
839+
And adding the z-axis
840+
841+
.. ipython:: python
842+
:okwarning:
795843
844+
@savefig ds_facet_scatter_z.png
845+
ds.plot.scatter(x="A", y="B", z="z", hue="y", markersize="x", row="x", col="w")
796846
797-
For more advanced scatter plots, we recommend converting the relevant data variables to a pandas DataFrame and using the extensive plotting capabilities of ``seaborn``.
847+
For more advanced scatter plots, we recommend converting the relevant data variables
848+
to a pandas DataFrame and using the extensive plotting capabilities of ``seaborn``.
798849

799850
Quiver
800851
~~~~~~
@@ -816,7 +867,8 @@ where ``u`` and ``v`` denote the x and y direction components of the arrow vecto
816867
@savefig ds_facet_quiver.png
817868
ds.plot.quiver(x="x", y="y", u="A", v="B", col="w", row="z", scale=4)
818869
819-
``scale`` is required for faceted quiver plots. The scale determines the number of data units per arrow length unit, i.e. a smaller scale parameter makes the arrow longer.
870+
``scale`` is required for faceted quiver plots.
871+
The scale determines the number of data units per arrow length unit, i.e. a smaller scale parameter makes the arrow longer.
820872

821873
Streamplot
822874
~~~~~~~~~~
@@ -830,7 +882,8 @@ Visualizing vector fields is also supported with streamline plots:
830882
ds.isel(w=1, z=1).plot.streamplot(x="x", y="y", u="A", v="B")
831883
832884
833-
where ``u`` and ``v`` denote the x and y direction components of the vectors tangent to the streamlines. Again, faceting is also possible:
885+
where ``u`` and ``v`` denote the x and y direction components of the vectors tangent to the streamlines.
886+
Again, faceting is also possible:
834887

835888
.. ipython:: python
836889
:okwarning:
@@ -983,7 +1036,7 @@ instead of the default ones:
9831036
)
9841037
9851038
@savefig plotting_example_2d_irreg.png width=4in
986-
da.plot.pcolormesh("lon", "lat")
1039+
da.plot.pcolormesh(x="lon", y="lat")
9871040
9881041
Note that in this case, xarray still follows the pixel centered convention.
9891042
This might be undesirable in some cases, for example when your data is defined
@@ -996,7 +1049,7 @@ this convention when plotting on a map:
9961049
import cartopy.crs as ccrs
9971050
9981051
ax = plt.subplot(projection=ccrs.PlateCarree())
999-
da.plot.pcolormesh("lon", "lat", ax=ax)
1052+
da.plot.pcolormesh(x="lon", y="lat", ax=ax)
10001053
ax.scatter(lon, lat, transform=ccrs.PlateCarree())
10011054
ax.coastlines()
10021055
@savefig plotting_example_2d_irreg_map.png width=4in
@@ -1009,7 +1062,7 @@ You can however decide to infer the cell boundaries and use the
10091062
:okwarning:
10101063
10111064
ax = plt.subplot(projection=ccrs.PlateCarree())
1012-
da.plot.pcolormesh("lon", "lat", ax=ax, infer_intervals=True)
1065+
da.plot.pcolormesh(x="lon", y="lat", ax=ax, infer_intervals=True)
10131066
ax.scatter(lon, lat, transform=ccrs.PlateCarree())
10141067
ax.coastlines()
10151068
@savefig plotting_example_2d_irreg_map_infer.png width=4in

doc/whats-new.rst

+13-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,22 @@ v2022.10.1 (unreleased)
2323
New Features
2424
~~~~~~~~~~~~
2525

26+
- Add static typing to plot accessors (:issue:`6949`, :pull:`7052`).
27+
By `Michael Niklas <https://github.com/headtr1ck>`_.
2628

2729
Breaking changes
2830
~~~~~~~~~~~~~~~~
2931

32+
- Many arguments of plotmethods have been made keyword-only.
33+
- ``xarray.plot.plot`` module renamed to ``xarray.plot.dataarray_plot`` to prevent
34+
shadowing of the ``plot`` method. (:issue:`6949`, :pull:`7052`).
35+
By `Michael Niklas <https://github.com/headtr1ck>`_.
3036

3137
Deprecations
3238
~~~~~~~~~~~~
3339

40+
- Positional arguments for all plot methods have been deprecated (:issue:`6949`, :pull:`7052`).
41+
By `Michael Niklas <https://github.com/headtr1ck>`_.
3442

3543
Bug fixes
3644
~~~~~~~~~
@@ -42,6 +50,9 @@ Documentation
4250

4351
Internal Changes
4452
~~~~~~~~~~~~~~~~
53+
- Doctests fail on any warnings (:pull:`7166`)
54+
By `Maximilian Roos <https://github.com/max-sixty>`_.
55+
4556

4657

4758
.. _whats-new.2022.10.0:
@@ -64,8 +75,8 @@ New Features
6475
the z argument. (:pull:`6778`)
6576
By `Jimmy Westling <https://github.com/illviljan>`_.
6677
- Include the variable name in the error message when CF decoding fails to allow
67-
for easier identification of problematic variables (:issue:`7145`,
68-
:pull:`7147`). By `Spencer Clark <https://github.com/spencerkclark>`_.
78+
for easier identification of problematic variables (:issue:`7145`, :pull:`7147`).
79+
By `Spencer Clark <https://github.com/spencerkclark>`_.
6980

7081
Breaking changes
7182
~~~~~~~~~~~~~~~~

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module = [
4848
"importlib_metadata.*",
4949
"iris.*",
5050
"matplotlib.*",
51+
"mpl_toolkits.*",
5152
"Nio.*",
5253
"nc_time_axis.*",
5354
"numbagg.*",

setup.cfg

-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ float_to_top = true
165165
default_section = THIRDPARTY
166166
known_first_party = xarray
167167

168-
169168
[aliases]
170169
test = pytest
171170

0 commit comments

Comments
 (0)