
.. 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_multivariate-eof.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_multivariate-eof.py>`
        to download the full example code

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

.. _sphx_glr_content_user_guide_auto_examples_1single_plot_multivariate-eof.py:


Multivariate EOF analysis
============================================

Multivariate EOF analysis.

.. GENERATED FROM PYTHON SOURCE LINES 7-24

.. code-block:: default


    # Load packages and data:
    import matplotlib.pyplot as plt
    import xarray as xr
    from cartopy.crs import PlateCarree
    from matplotlib.gridspec import GridSpec

    import xeofs as xe

    # Create four different dataarrayss
    sst = xr.tutorial.open_dataset("ersstv5")["sst"]
    subset1 = sst.isel(lon=slice(0, 45))
    subset2 = sst.isel(lon=slice(46, 90))
    subset3 = sst.isel(lon=slice(91, 135))
    subset4 = sst.isel(lon=slice(136, None))
    multivariate_data = [subset1, subset2, subset3, subset4]








.. GENERATED FROM PYTHON SOURCE LINES 25-26

Perform the actual analysis

.. GENERATED FROM PYTHON SOURCE LINES 26-32

.. code-block:: default


    pca = xe.single.EOF(n_modes=10, standardize=False, use_coslat=True)
    pca.fit(multivariate_data, dim="time")
    components = pca.components()
    scores = pca.scores()








.. GENERATED FROM PYTHON SOURCE LINES 33-34

Plot mode 1

.. GENERATED FROM PYTHON SOURCE LINES 34-68

.. code-block:: default


    mode = 5
    proj = PlateCarree()
    kwargs = {
        "cmap": "RdBu",
        "vmin": -0.05,
        "vmax": 0.05,
        "transform": proj,
        "add_colorbar": False,
    }

    fig = plt.figure(figsize=(7.3, 6))
    fig.subplots_adjust(wspace=0)
    gs = GridSpec(2, 4, figure=fig, width_ratios=[1, 1, 1, 1])
    ax = [fig.add_subplot(gs[0, i], projection=proj) for i in range(4)]
    ax_pc = fig.add_subplot(gs[1, :])

    # PC
    scores.sel(mode=mode).plot(ax=ax_pc)
    ax_pc.set_xlabel("")
    ax_pc.set_title("")

    # EOFs
    for i, (a, comps) in enumerate(zip(ax, components)):
        a.coastlines(color=".5")
        comps.sel(mode=mode).plot(ax=a, **kwargs)
        a.set_xticks([], [])
        a.set_yticks([], [])
        a.set_xlabel("")
        a.set_ylabel("")
        a.set_title("Subset {:}".format(i + 1))
    ax[0].set_ylabel("EOFs")
    fig.suptitle("Mode {:}".format(mode))
    plt.savefig("multivariate-eof-analysis.jpg")



.. image-sg:: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_multivariate-eof_001.png
   :alt: Mode 5, Subset 1, Subset 2, Subset 3, Subset 4
   :srcset: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_multivariate-eof_001.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_content_user_guide_auto_examples_1single_plot_multivariate-eof.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_multivariate-eof.py <plot_multivariate-eof.py>`

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

      :download:`Download Jupyter notebook: plot_multivariate-eof.ipynb <plot_multivariate-eof.ipynb>`


.. only:: html

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

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