
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "content/user_guide/auto_examples/1single/plot_eof-tmode.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_content_user_guide_auto_examples_1single_plot_eof-tmode.py>`
        to download the full example code

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

.. _sphx_glr_content_user_guide_auto_examples_1single_plot_eof-tmode.py:


EOF analysis (T-mode)
========================

EOF analysis in T-mode maximises the spatial variance.

Load packages and data:

.. GENERATED FROM PYTHON SOURCE LINES 9-19

.. code-block:: default


    import matplotlib.pyplot as plt
    import xarray as xr
    from cartopy.crs import EqualEarth, PlateCarree
    from matplotlib.gridspec import GridSpec

    import xeofs as xe

    sst = xr.tutorial.open_dataset("ersstv5")["sst"]








.. GENERATED FROM PYTHON SOURCE LINES 20-21

Perform the actual analysis

.. GENERATED FROM PYTHON SOURCE LINES 21-28

.. code-block:: default


    model = xe.single.EOF(n_modes=5)
    model.fit(sst, dim=("lat", "lon"))
    expvar = model.explained_variance_ratio()
    components = model.components()
    scores = model.scores()








.. GENERATED FROM PYTHON SOURCE LINES 29-30

Create figure showing the first two modes

.. GENERATED FROM PYTHON SOURCE LINES 30-48

.. code-block:: default


    proj = EqualEarth(central_longitude=180)
    kwargs = {"cmap": "RdBu", "transform": PlateCarree()}

    fig = plt.figure(figsize=(10, 8))
    gs = GridSpec(3, 2, width_ratios=[2, 1])
    ax0 = [fig.add_subplot(gs[i, 0], projection=proj) for i in range(3)]
    ax1 = [fig.add_subplot(gs[i, 1]) for i in range(3)]

    for i, (a0, a1) in enumerate(zip(ax0, ax1)):
        scores.sel(mode=i + 1).plot(ax=a0, **kwargs)
        a0.coastlines(color=".5")
        components.sel(mode=i + 1).plot(ax=a1)

        a0.set_xlabel("")

    plt.tight_layout()
    plt.savefig("eof-tmode.jpg")



.. image-sg:: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_eof-tmode_001.png
   :alt: mode = 1, mode = 2, mode = 3, mode = 1, mode = 2, mode = 3
   :srcset: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_eof-tmode_001.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_content_user_guide_auto_examples_1single_plot_eof-tmode.py:

.. only:: html

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




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

      :download:`Download Python source code: plot_eof-tmode.py <plot_eof-tmode.py>`

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

      :download:`Download Jupyter notebook: plot_eof-tmode.ipynb <plot_eof-tmode.ipynb>`


.. only:: html

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

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