.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/plotting.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_plotting.py: Plot unstructured mesh data =========================== The labels that are present in xarray's data structures allow for easy creation of informative plots: think of dates on the x-axis, or geospatial coordinates. Xarray provides a convenient way of plotting your data provided it is structured. Xugrid extends these plotting methods to easily make spatial (x-y) plots of unstructured grids. Like Xarray's focus for plotting is the DataArray, Xugrid's focus is the UgridDataArray; like Xarray, if your (extracted) data fits into a pandas DataFrame, you're better of using pandas tools instead. As every other method in Xugrid, any logic involving the unstructured topology is accessed via the ``.ugrid`` accessor on the DataArrays and Datasets; UgridDatasets and UgridDataArrays behave the same as ordinary Xarray DataArrays and Datasets otherwise. Imports ------- The following imports suffice for the examples. .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. code-block:: Python import matplotlib.pyplot as plt import xugrid .. GENERATED FROM PYTHON SOURCE LINES 32-41 We'll use a simple synthetic example. This dataset contains data for all topological attributes of a two dimensional mesh: * Nodes: the coordinate pair (x, y) forming a point. * Edges: a line or curve bounded by two nodes. * Faces: the polygon enclosed by a set of edges. In this disk example, very similar has been placed on the nodes, edges, and faces. .. GENERATED FROM PYTHON SOURCE LINES 41-45 .. code-block:: Python ds = xugrid.data.disk() ds .. raw:: html
<xarray.Dataset> Size: 19kB
    Dimensions:        (mesh2d_nNodes: 217, mesh2d_nFaces: 384, mesh2d_nEdges: 600)
    Coordinates:
      * mesh2d_nNodes  (mesh2d_nNodes) int64 2kB 0 1 2 3 4 5 ... 212 213 214 215 216
      * mesh2d_nFaces  (mesh2d_nFaces) int64 3kB 0 1 2 3 4 5 ... 379 380 381 382 383
      * mesh2d_nEdges  (mesh2d_nEdges) int64 5kB 0 1 2 3 4 5 ... 595 596 597 598 599
    Data variables:
        node_z         (mesh2d_nNodes) float64 2kB 1.933 2.091 1.875 ... 5.688 7.491
        face_z         (mesh2d_nFaces) float64 3kB 1.737 1.918 2.269 ... 5.408 6.424
        edge_z         (mesh2d_nEdges) float64 5kB 1.989 1.875 1.8 ... 4.909 6.544


.. GENERATED FROM PYTHON SOURCE LINES 46-51 UgridDataArray -------------- Just like Xarray, we can create a plot by selecting a DataArray from the Dataset and calling the :py:meth:`UgridDataArray.ugrid.plot()` method. .. GENERATED FROM PYTHON SOURCE LINES 51-55 .. code-block:: Python uda = ds["face_z"] uda.ugrid.plot() .. image-sg:: /examples/images/sphx_glr_plotting_001.png :alt: plotting :srcset: /examples/images/sphx_glr_plotting_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 56-63 Like Xarray, the axes and the colorbar are labeled automatically using the available information. The convenience function :py:meth:`xugrid.UgridDataArray.ugrid.plot()` dispatches on the topological dimension of the variable. In this case, the data is associated with the face dimension of the topology. Data located on the edges results in a different kind of plot: .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. code-block:: Python ds["edge_z"].ugrid.plot() .. image-sg:: /examples/images/sphx_glr_plotting_002.png :alt: plotting :srcset: /examples/images/sphx_glr_plotting_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 67-78 The method called by default depends on the type of the data: =============== =========================== Dimension Plotting function =============== =========================== Face :py:func:`xugrid.plot.pcolormesh` Edge :py:func:`xugrid.plot.line` Node :py:func:`xugrid.plot.tripcolor` =============== =========================== We can put them side by side to illustrate the differences: .. GENERATED FROM PYTHON SOURCE LINES 78-84 .. code-block:: Python fig, (ax0, ax1, ax2) = plt.subplots(ncols=3, figsize=(11, 3), sharex=True, sharey=True) ds["face_z"].ugrid.plot(ax=ax0) ds["edge_z"].ugrid.plot(ax=ax1) ds["node_z"].ugrid.plot(ax=ax2) .. image-sg:: /examples/images/sphx_glr_plotting_003.png :alt: plotting :srcset: /examples/images/sphx_glr_plotting_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 85-87 We can also exactly control the type of plot we want. For example, to plot filled contours for data associated with the face dimension: .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: Python ds["face_z"].ugrid.plot.contourf() .. image-sg:: /examples/images/sphx_glr_plotting_004.png :alt: plotting :srcset: /examples/images/sphx_glr_plotting_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 91-92 We can also overlay this data with the edges: .. GENERATED FROM PYTHON SOURCE LINES 92-97 .. code-block:: Python fig, ax = plt.subplots() ds["face_z"].ugrid.plot.contourf() ds["face_z"].ugrid.plot.line(color="black") .. image-sg:: /examples/images/sphx_glr_plotting_005.png :alt: plotting :srcset: /examples/images/sphx_glr_plotting_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 98-131 In general, there has to be data associated with the mesh topology before a plot can be made. ``plot.line()`` forms an exception to this rule, as the location of the edges is meaningful on its own: for this reason ``plot.line()`` does not error in the example above. Other types of plot ------------------- The available plotting methods per topology dimension are listed here. For the **face** dimension: * :py:func:`xugrid.plot.contour` * :py:func:`xugrid.plot.contourf` * :py:func:`xugrid.plot.imshow` * :py:func:`xugrid.plot.pcolormesh` * :py:func:`xugrid.plot.scatter` * :py:func:`xugrid.plot.surface` For the **edge** dimension: * :py:func:`xugrid.plot.line` * :py:func:`xugrid.plot.scatter` For the **node** dimension: * :py:func:`xugrid.plot.contour` * :py:func:`xugrid.plot.contourf` * :py:func:`xugrid.plot.scatter` * :py:func:`xugrid.plot.surface` * :py:func:`xugrid.plot.tripcolor` All these (2D) plots are illustrated here for completeness' sake: .. GENERATED FROM PYTHON SOURCE LINES 131-148 .. code-block:: Python fig, axes = plt.subplots(nrows=5, ncols=3, figsize=(10, 15)) ds["face_z"].ugrid.plot.pcolormesh(ax=axes[0, 0]) ds["face_z"].ugrid.plot.contour(ax=axes[1, 0]) ds["face_z"].ugrid.plot.contourf(ax=axes[2, 0]) ds["face_z"].ugrid.plot.imshow(ax=axes[3, 0]) ds["face_z"].ugrid.plot.scatter(ax=axes[4, 0]) ds["edge_z"].ugrid.plot.line(ax=axes[0, 1]) ds["edge_z"].ugrid.plot.scatter(ax=axes[4, 1]) ds["node_z"].ugrid.plot.tripcolor(ax=axes[0, 2]) ds["node_z"].ugrid.plot.contour(ax=axes[1, 2]) ds["node_z"].ugrid.plot.contourf(ax=axes[2, 2]) ds["node_z"].ugrid.plot.scatter(ax=axes[4, 2]) .. image-sg:: /examples/images/sphx_glr_plotting_006.png :alt: plotting :srcset: /examples/images/sphx_glr_plotting_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 149-150 The ``surface`` methods generate 3D surface plots: .. GENERATED FROM PYTHON SOURCE LINES 150-157 .. code-block:: Python fig = plt.figure(figsize=plt.figaspect(0.5)) ax0 = fig.add_subplot(1, 2, 1, projection="3d") ax1 = fig.add_subplot(1, 2, 2, projection="3d") ds["face_z"].ugrid.plot.surface(ax=ax0) ds["node_z"].ugrid.plot.surface(ax=ax1) .. image-sg:: /examples/images/sphx_glr_plotting_007.png :alt: plotting :srcset: /examples/images/sphx_glr_plotting_007.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 158-164 Additional Arguments -------------------- Once again like in Xarray, additional arguments are passed to the underlying matplotlib function and the additional arguments supported by Xarray can be used: .. GENERATED FROM PYTHON SOURCE LINES 164-167 .. code-block:: Python ds["face_z"].ugrid.plot(cmap="RdBu", levels=8, yincrease=False) .. image-sg:: /examples/images/sphx_glr_plotting_008.png :alt: plotting :srcset: /examples/images/sphx_glr_plotting_008.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 168-173 As a function ------------- The plotting methods can also be called as a function, in which case they take an xarray DataArray and a xugrid grid as arguments. .. GENERATED FROM PYTHON SOURCE LINES 173-179 .. code-block:: Python grid = ds.ugrid.grids[0] da = ds.obj["face_z"] xugrid.plot.pcolormesh(grid, da) .. image-sg:: /examples/images/sphx_glr_plotting_009.png :alt: plotting :srcset: /examples/images/sphx_glr_plotting_009.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 180-186 Xarray DataArray plots ---------------------- As mentioned, apart from the ``.ugrid`` accessor, a UgridDataArray behaves the same as an Xarray DataArray. To illustrate, we can select a location somewhere in the unstructured topology, and plot the resulting timeseries: .. GENERATED FROM PYTHON SOURCE LINES 186-191 .. code-block:: Python ds = xugrid.data.adh_san_diego() depth = ds["depth"] depth.isel(node=1000).plot() .. image-sg:: /examples/images/sphx_glr_plotting_010.png :alt: node_x = 4.84e+05, node_y = 3.614e+06, node = 1000 :srcset: /examples/images/sphx_glr_plotting_010.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 13.823 seconds) .. _sphx_glr_download_examples_plotting.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plotting.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plotting.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plotting.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_