.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/2multi/plot_mca.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_auto_examples_2multi_plot_mca.py: Maximum Covariance Analysis =========================== Maximum Covariance Analysis (MCA) between two data sets. .. GENERATED FROM PYTHON SOURCE LINES 7-18 .. code-block:: Python # Load packages and data: import numpy as np import xarray as xr import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec from cartopy.crs import Orthographic, PlateCarree from cartopy.feature import LAND from xeofs.models import MCA .. GENERATED FROM PYTHON SOURCE LINES 19-20 Create 2 different DataArrays .. GENERATED FROM PYTHON SOURCE LINES 20-25 .. code-block:: Python t2m = xr.tutorial.load_dataset("air_temperature")["air"] da1 = t2m.isel(lon=slice(0, 26)) da2 = t2m.isel(lon=slice(27, None)) .. GENERATED FROM PYTHON SOURCE LINES 26-27 Perform MCA .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. code-block:: Python mca = MCA(n_modes=20, standardize=False, use_coslat=True) mca.fit(da1, da2, dim="time") .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 32-34 Get singular vectors, projections (PCs), homogeneous and heterogeneous patterns: .. GENERATED FROM PYTHON SOURCE LINES 34-40 .. code-block:: Python singular_vectors = mca.components() scores = mca.scores() hom_pats, pvals_hom = mca.homogeneous_patterns() het_pats, pvals_het = mca.heterogeneous_patterns() .. GENERATED FROM PYTHON SOURCE LINES 41-45 When two fields are expected, the output of the above methods is a list of length 2, with the first and second entry containing the relevant object for ``X`` and ``Y``. For example, the p-values obtained from the two-sided t-test for the homogeneous patterns of ``X`` are: .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: Python pvals_hom[0] .. raw:: html
<xarray.DataArray 'pvalues_of_left_homogeneous_patterns' (mode: 20, lat: 25,
                                                              lon: 26)> Size: 104kB
    7.302e-298 4.926e-268 8.327e-265 3.64e-251 ... 0.04256 0.08683 0.1689 0.2897
    Coordinates:
      * lat      (lat) float32 100B 15.0 17.5 20.0 22.5 25.0 ... 67.5 70.0 72.5 75.0
      * lon      (lon) float32 104B 200.0 202.5 205.0 207.5 ... 257.5 260.0 262.5
      * mode     (mode) int64 160B 1 2 3 4 5 6 7 8 9 ... 12 13 14 15 16 17 18 19 20
    Attributes: (12/16)
        model:          MCA
        software:       xeofs
        version:        2.3.2
        date:           2024-03-31 21:13:25
        n_modes:        20
        center:         True
        ...             ...
        compute:        True
        sample_name:    sample
        feature_name:   feature
        solver:         auto
        random_state:   None
        solver_kwargs:  {}


.. GENERATED FROM PYTHON SOURCE LINES 49-50 Create a mask to identifiy where p-values are below 0.05 .. GENERATED FROM PYTHON SOURCE LINES 50-55 .. code-block:: Python hom_mask = [values < 0.05 for values in pvals_hom] het_mask = [values < 0.05 for values in pvals_het] .. GENERATED FROM PYTHON SOURCE LINES 56-57 Plot some relevant quantities of mode 2. .. GENERATED FROM PYTHON SOURCE LINES 57-113 .. code-block:: Python lonlats = [ np.meshgrid(pvals_hom[0].lon.values, pvals_hom[0].lat.values), np.meshgrid(pvals_hom[1].lon.values, pvals_hom[1].lat.values), ] proj = [ Orthographic(central_latitude=30, central_longitude=-120), Orthographic(central_latitude=30, central_longitude=-60), ] kwargs1 = {"cmap": "BrBG", "vmin": -0.05, "vmax": 0.05, "transform": PlateCarree()} kwargs2 = {"cmap": "RdBu", "vmin": -1, "vmax": 1, "transform": PlateCarree()} mode = 2 fig = plt.figure(figsize=(7, 14)) gs = GridSpec(5, 2) ax1 = [fig.add_subplot(gs[0, i], projection=proj[i]) for i in range(2)] ax2 = [fig.add_subplot(gs[1, i], projection=proj[i]) for i in range(2)] ax3 = [fig.add_subplot(gs[2, i], projection=proj[i]) for i in range(2)] ax4 = [fig.add_subplot(gs[3, i]) for i in range(2)] for i, a in enumerate(ax1): singular_vectors[i].sel(mode=mode).plot(ax=a, **kwargs1) for i, a in enumerate(ax2): hom_pats[i].sel(mode=mode).plot(ax=a, **kwargs2) a.scatter( lonlats[i][0], lonlats[i][1], hom_mask[i].sel(mode=mode).values * 0.5, color="k", alpha=0.5, transform=PlateCarree(), ) for i, a in enumerate(ax3): het_pats[i].sel(mode=mode).plot(ax=a, **kwargs2) a.scatter( lonlats[i][0], lonlats[i][1], het_mask[i].sel(mode=mode).values * 0.5, color="k", alpha=0.5, transform=PlateCarree(), ) for i, a in enumerate(ax4): scores[i].sel(mode=mode).plot(ax=a) a.set_xlabel("") for a in np.ravel([ax1, ax2, ax3]): a.coastlines(color=".5") a.add_feature(LAND) plt.tight_layout() plt.savefig("mca.jpg") .. image-sg:: /auto_examples/2multi/images/sphx_glr_plot_mca_001.png :alt: mode = 2, mode = 2, mode = 2, mode = 2, mode = 2, mode = 2, mode = 2, mode = 2 :srcset: /auto_examples/2multi/images/sphx_glr_plot_mca_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/slevang/miniconda3/envs/xeofs-docs/lib/python3.11/site-packages/cartopy/io/__init__.py:241: DownloadWarning: Downloading: https://naturalearth.s3.amazonaws.com/110m_physical/ne_110m_land.zip warnings.warn(f'Downloading: {url}', DownloadWarning) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.519 seconds) .. _sphx_glr_download_auto_examples_2multi_plot_mca.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_mca.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_mca.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_