You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/user-guide/plotting.rst
+79-26
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ Matplotlib must be installed before xarray can plot.
27
27
28
28
To use xarray's plotting capabilities with time coordinates containing
29
29
``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
31
31
needs to be installed.
32
32
33
33
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
106
106
@savefigplotting_1d_simple.pngwidth=4in
107
107
air1d.plot()
108
108
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
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()``,
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.
354
364
355
365
.. ipython:: python
356
366
:okwarning:
@@ -585,7 +595,10 @@ Faceting here refers to splitting an array along one or two dimensions and
585
595
plotting each group.
586
596
Xarray's basic plotting is useful for plotting two dimensional arrays. What
587
597
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”.
589
602
590
603
Consider the temperature data set. There are 4 observations per day for two
591
604
years which makes for 2920 values along the time dimension.
@@ -670,8 +683,8 @@ Faceted plotting supports other arguments common to xarray 2d plots.
670
683
671
684
@savefigplot_facet_robust.png
672
685
g = hasoutliers.plot.pcolormesh(
673
-
"lon",
674
-
"lat",
686
+
x="lon",
687
+
y="lat",
675
688
col="time",
676
689
col_wrap=3,
677
690
robust=True,
@@ -711,7 +724,7 @@ they have been plotted.
711
724
.. ipython:: python
712
725
:okwarning:
713
726
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)
715
728
716
729
for i, ax inenumerate(g.axes.flat):
717
730
ax.set_title("Air Temperature %d"% i)
@@ -727,7 +740,8 @@ they have been plotted.
727
740
axis labels, axis ticks and plot titles. See :py:meth:`~xarray.plot.FacetGrid.set_titles`,
728
741
:py:meth:`~xarray.plot.FacetGrid.set_xlabels`, :py:meth:`~xarray.plot.FacetGrid.set_ylabels` and
729
742
: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`.
731
745
732
746
TODO: add an example of using the ``map`` method to plot dataset variables
733
747
(e.g., with ``plt.quiver``).
@@ -742,14 +756,32 @@ Consider this dataset
742
756
743
757
.. ipython:: python
744
758
745
-
ds = xr.tutorial.scatter_example_dataset()
759
+
ds = xr.tutorial.scatter_example_dataset(seed=42)
746
760
ds
747
761
748
762
749
763
Scatter
750
764
~~~~~~~
751
765
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
+
@savefigda_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
+
@savefigds_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
753
785
754
786
.. ipython:: python
755
787
:okwarning:
@@ -765,36 +797,55 @@ The ``hue`` kwarg lets you vary the color by variable value
765
797
@savefigds_hue_scatter.png
766
798
ds.plot.scatter(x="A", y="B", hue="w")
767
799
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``.
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.
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``.
798
849
799
850
Quiver
800
851
~~~~~~
@@ -816,7 +867,8 @@ where ``u`` and ``v`` denote the x and y direction components of the arrow vecto
``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.
820
872
821
873
Streamplot
822
874
~~~~~~~~~~
@@ -830,7 +882,8 @@ Visualizing vector fields is also supported with streamline plots:
0 commit comments