
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "tutorials/graphics/plot_taylor_diagram.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_tutorials_graphics_plot_taylor_diagram.py>`
        to download the full example code or to run this example in your browser via Binder.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_tutorials_graphics_plot_taylor_diagram.py:


Compare simulations with a reference using Taylor diagrams
==========================================================

In this tutorial, we show how to:

* plot one point per model, with a legend and different markers,
* add drop shadows to the markers,
* color the points with values,
* handle negative correlations,
* draw already normalized statistics without a reference array.

The concepts are explained in the :ref:`indepth.plot` guide.
See also: :ref:`sphx_glr_tutorials_evaluation_plot_stataccum.py`.

.. GENERATED FROM PYTHON SOURCE LINES 19-21

Initialisations
---------------

.. GENERATED FROM PYTHON SOURCE LINES 21-31

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np
    import xarray as xr

    from xoa.core import plot as cplot
    from xoa.plot import add_shadow, plot_taylor

    plt.rcParams.update({"font.size": 9, "axes.titlesize": 10, "axes.titleweight": "bold"})








.. GENERATED FROM PYTHON SOURCE LINES 32-34

Synthetic data: a reference and models that are more or less correlated with it,
on dimensions ``time`` and ``x``.

.. GENERATED FROM PYTHON SOURCE LINES 34-48

.. code-block:: Python


    rng = np.random.default_rng(0)
    ref = xr.DataArray(
        np.sin(np.linspace(0, 12, 200))[:, None] + 0.3 * rng.normal(size=(200, 10)),
        dims=("time", "x"),
        attrs={"units": "m"},
    )
    noise = [0.2, 0.5, 1.0, 1.5]
    scales = [1.0, 1.2, 0.7, 1.4]
    models = xr.concat(
        [s * ref + n * rng.normal(size=ref.shape) for s, n in zip(scales, noise)],
        dim=xr.DataArray(["MARS", "CROCO", "HYCOM", "NEMO"], dims="model", name="model"),
    )








.. GENERATED FROM PYTHON SOURCE LINES 49-57

One point per model
-------------------
:func:`~xoa.plot.plot_taylor` computes the statistics over ``time`` and ``x``.
The remaining ``model`` dimension creates the points, whose labels are the coordinates.
A blurred drop shadow is added to the markers with :func:`xoa.plot.add_shadow`.
The artists of the diagram are available as attributes, like ``markers`` and ``reference``.
It must be called in the same cell as the plot, since the figure is captured
at the end of the cell.

.. GENERATED FROM PYTHON SOURCE LINES 57-63

.. code-block:: Python


    diagram = plot_taylor(models, ref, dim=("time", "x"), markers=["o", "s", "^", "D"])
    add_shadow(
        diagram.markers + [diagram.reference], width=3, xoffset=2, yoffset=-2, alpha=0.4, ax=diagram.ax
    )




.. image-sg:: /tutorials/graphics/images/sphx_glr_plot_taylor_diagram_001.png
   :alt: plot taylor diagram
   :srcset: /tutorials/graphics/images/sphx_glr_plot_taylor_diagram_001.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    <xoa.plot.FilteredArtistList object at 0x738c05346d50>



.. GENERATED FROM PYTHON SOURCE LINES 64-67

Normalized, with colored points
-------------------------------
With ``values``, the markers are colored and the labels are written next to them.

.. GENERATED FROM PYTHON SOURCE LINES 67-71

.. code-block:: Python


    cost = xr.DataArray([3.0, 5.0, 2.0, 8.0], dims="model", attrs={"long_name": "Cost", "units": "h"})
    plot_taylor(models, ref, dim=("time", "x"), normalize=True, values=cost, cmap="viridis")




.. image-sg:: /tutorials/graphics/images/sphx_glr_plot_taylor_diagram_002.png
   :alt: plot taylor diagram
   :srcset: /tutorials/graphics/images/sphx_glr_plot_taylor_diagram_002.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    <xoa.core.plot.TaylorDiagram object at 0x738c05345090>



.. GENERATED FROM PYTHON SOURCE LINES 72-75

Negative correlations
---------------------
The diagram becomes a half circle when needed.

.. GENERATED FROM PYTHON SOURCE LINES 75-86

.. code-block:: Python


    diagram = plot_taylor(
        xr.concat([models, -models.isel(model=[1])], dim="model"),
        ref,
        dim=("time", "x"),
        labels=["MARS", "CROCO", "HYCOM", "NEMO", "-CROCO"],
        normalize=True,
    )
    diagram.ax.figure.set_size_inches(8, 4.3)
    diagram.ax.figure.subplots_adjust(left=0.02, right=0.82, top=0.9, bottom=0.13)




.. image-sg:: /tutorials/graphics/images/sphx_glr_plot_taylor_diagram_003.png
   :alt: plot taylor diagram
   :srcset: /tutorials/graphics/images/sphx_glr_plot_taylor_diagram_003.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 87-90

Without a reference array
-------------------------
Normalized statistics can be drawn directly, the reference standard deviation being 1.

.. GENERATED FROM PYTHON SOURCE LINES 90-94

.. code-block:: Python


    diagram = cplot.plot_taylor([0.9, 1.2, 0.7], [0.95, 0.8, -0.3], labels=["a", "b", "c"], rmax=1.5)
    diagram.ax.figure.set_size_inches(8, 4.3)
    diagram.ax.figure.subplots_adjust(left=0.02, right=0.82, top=0.9, bottom=0.13)



.. image-sg:: /tutorials/graphics/images/sphx_glr_plot_taylor_diagram_004.png
   :alt: plot taylor diagram
   :srcset: /tutorials/graphics/images/sphx_glr_plot_taylor_diagram_004.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 1.208 seconds)


.. _sphx_glr_download_tutorials_graphics_plot_taylor_diagram.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: binder-badge

      .. image:: images/binder_badge_logo.svg
        :target: https://mybinder.org/v2/gh/shom-fr/xoa/master?urlpath=lab/tree/notebooks/tutorials/graphics/plot_taylor_diagram.ipynb
        :alt: Launch binder
        :width: 150 px

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_taylor_diagram.ipynb <plot_taylor_diagram.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_taylor_diagram.py <plot_taylor_diagram.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_taylor_diagram.zip <plot_taylor_diagram.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
