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

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

.. _sphx_glr_content_user_guide_auto_examples_1single_plot_gwpca.py:


Geographically weighted PCA
===========================
Geographically Weighted Principal Component Analysis (GWPCA) is a spatial analysis method that identifies and visualizes local spatial patterns and relationships in multivariate datasets across various geographic areas. It operates by applying PCA within a moving window over a geographical region, which enables the extraction of local principal components that can differ across locations.

TIn this demonstration, we'll apply GWPCA to a dataset detailing the chemical compositions of soils from countries around the Baltic Sea [1]_. This example is inspired by a tutorial originally crafted and published by Chris Brunsdon [2]_.
The dataset comprises 10 variables (chemical elements) and spans 768 samples.
Here, each sample refers to a pair of latitude and longitude coordinates, representing specific sampling stations.

.. [1] Reimann, C. et al. Baltic soil survey: total concentrations of major and selected trace elements in arable soils from 10 countries around the Baltic Sea. Science of The Total Environment 257, 155–170 (2000).
.. [2] https://rpubs.com/chrisbrunsdon/99675



.. note:: The dataset we're using is found in the R package
    `mvoutlier <https://cran.r-project.org/web/packages/mvoutlier/mvoutlier.pdf>`_.
    To access it, we'll employ the Python package
    `rpy2 <https://rpy2.github.io/doc/latest/html/index.html>`_ which facilitates
    interaction with R packages from within Python.

.. note:: Presently, there's no support for ``xarray.Dataset`` lacking an explicit feature dimension.
    As a workaround, ``xarray.DataArray.to_array`` can be used to convert the ``Dataset`` to an ``DataArray``.

.. warning:: Bear in mind that GWPCA requires significant computational power.
    The ``xeofs`` implementation is optimized for CPU efficiency and is best suited
    for smaller to medium data sets. For more extensive datasets where parallel processing becomes essential,
    it's advisable to turn to the R package `GWmodel <https://cran.r-project.org/web/packages/GWmodel/GWmodel.pdf>`_.
    This package harnesses CUDA to enable GPU-accelerated GWPCA for optimized performance.


Let's import the necessary packages.

.. GENERATED FROM PYTHON SOURCE LINES 33-48

.. code-block:: default


    # For the analysis
    # For visualization
    import matplotlib.pyplot as plt
    import numpy as np

    # For accessing R packages
    import rpy2.robjects as ro
    import seaborn as sns
    import xarray as xr
    from rpy2.robjects import pandas2ri
    from rpy2.robjects.packages import importr

    import xeofs as xe








.. GENERATED FROM PYTHON SOURCE LINES 49-51

Next, we'll install the R package `mvoutlier <https://cran.r-project.org/web/packages/mvoutlier/mvoutlier.pdf>`_
using the `rpy2 <https://rpy2.github.io/doc/latest/html/index.html>`_ package.

.. GENERATED FROM PYTHON SOURCE LINES 51-57

.. code-block:: default


    xr.set_options(display_expand_data=False)
    utils = importr("utils")
    utils.chooseCRANmirror(ind=1)
    utils.install_packages("mvoutlier")





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

 .. code-block:: none

    R[write to console]: trying URL 'https://cloud.r-project.org/src/contrib/mvoutlier_2.1.1.tar.gz'

    R[write to console]: Content type 'application/x-gzip'
    R[write to console]:  length 476636 bytes (465 KB)

    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: =
    R[write to console]: 

    R[write to console]: downloaded 465 KB


    R[write to console]: 

    R[write to console]: 
    R[write to console]: The downloaded source packages are in
            ‘/tmp/RtmpHyr0o2/downloaded_packages’
    R[write to console]: 
    R[write to console]: 

    R[write to console]: Updating HTML index of packages in '.Library'

    R[write to console]: Making 'packages.html' ...
    R[write to console]:  done


    <rpy2.rinterface_lib.sexp.NULLType object at 0x7e8599786c10> [0]



.. GENERATED FROM PYTHON SOURCE LINES 58-61

Let's load the dataset and convert it into a ``pandas.DataFrame``.
Alongside, we'll also load the background data that outlines the borders of countries
in the Baltic Sea region. This will help us visually represent the GWPCA results.

.. GENERATED FROM PYTHON SOURCE LINES 61-75

.. code-block:: default


    ro.r(
        """
        require("mvoutlier")
        data(bsstop)
        Data <- bsstop[,1:14]
        background <- bss.background
        """
    )
    with (ro.default_converter + pandas2ri.converter).context():
        data_df = ro.conversion.get_conversion().rpy2py(ro.r["Data"])
        background_df = ro.conversion.get_conversion().rpy2py(ro.r["background"])
    data_df.head()





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

 .. code-block:: none

    R[write to console]: Loading required package: mvoutlier

    R[write to console]: Loading required package: sgeostat



.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <div>
    <style scoped>
        .dataframe tbody tr th:only-of-type {
            vertical-align: middle;
        }

        .dataframe tbody tr th {
            vertical-align: top;
        }

        .dataframe thead th {
            text-align: right;
        }
    </style>
    <table border="1" class="dataframe">
      <thead>
        <tr style="text-align: right;">
          <th></th>
          <th>ID</th>
          <th>CNo</th>
          <th>XCOO</th>
          <th>YCOO</th>
          <th>SiO2_T</th>
          <th>TiO2_T</th>
          <th>Al2O3_T</th>
          <th>Fe2O3_T</th>
          <th>MnO_T</th>
          <th>MgO_T</th>
          <th>CaO_T</th>
          <th>Na2O_T</th>
          <th>K2O_T</th>
          <th>P2O5_T</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th>1</th>
          <td>5001.0</td>
          <td>60.0</td>
          <td>-619656.5</td>
          <td>6805304.1</td>
          <td>43.61</td>
          <td>1.290</td>
          <td>13.07</td>
          <td>12.25</td>
          <td>0.167</td>
          <td>3.22</td>
          <td>2.48</td>
          <td>1.14</td>
          <td>2.01</td>
          <td>0.481</td>
        </tr>
        <tr>
          <th>2</th>
          <td>5002.0</td>
          <td>120.0</td>
          <td>214714.1</td>
          <td>7745546.6</td>
          <td>58.73</td>
          <td>0.913</td>
          <td>14.78</td>
          <td>6.48</td>
          <td>0.105</td>
          <td>2.47</td>
          <td>3.08</td>
          <td>2.19</td>
          <td>1.78</td>
          <td>0.298</td>
        </tr>
        <tr>
          <th>3</th>
          <td>5003.0</td>
          <td>33.0</td>
          <td>-368415.5</td>
          <td>7065039.2</td>
          <td>58.14</td>
          <td>0.902</td>
          <td>11.89</td>
          <td>5.70</td>
          <td>0.126</td>
          <td>2.44</td>
          <td>3.17</td>
          <td>2.13</td>
          <td>1.16</td>
          <td>0.408</td>
        </tr>
        <tr>
          <th>4</th>
          <td>5004.0</td>
          <td>39.0</td>
          <td>226609.0</td>
          <td>6922431.0</td>
          <td>43.98</td>
          <td>0.524</td>
          <td>10.00</td>
          <td>4.08</td>
          <td>0.052</td>
          <td>1.00</td>
          <td>1.37</td>
          <td>1.60</td>
          <td>1.82</td>
          <td>0.395</td>
        </tr>
        <tr>
          <th>5</th>
          <td>5005.0</td>
          <td>103.0</td>
          <td>544050.0</td>
          <td>7808760.0</td>
          <td>60.90</td>
          <td>0.702</td>
          <td>13.20</td>
          <td>6.37</td>
          <td>0.079</td>
          <td>2.59</td>
          <td>3.13</td>
          <td>2.97</td>
          <td>1.35</td>
          <td>0.139</td>
        </tr>
      </tbody>
    </table>
    </div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 76-77

Since ``xeofs`` uses ``xarray``, we convert the data into an ``xarray.DataArray``.

.. GENERATED FROM PYTHON SOURCE LINES 77-86

.. code-block:: default


    data_df = data_df.rename(columns={"ID": "station"}).set_index("station")
    data = data_df.to_xarray()
    data = data.rename({"XCOO": "x", "YCOO": "y"})
    data = data.set_index(station=("x", "y"))
    data = data.drop_vars("CNo")
    da = data.to_array(dim="element")
    da






.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <div><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
    <defs>
    <symbol id="icon-database" viewBox="0 0 32 32">
    <path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
    <path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
    <path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
    </symbol>
    <symbol id="icon-file-text2" viewBox="0 0 32 32">
    <path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
    <path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
    <path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
    <path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
    </symbol>
    </defs>
    </svg>
    <style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
     *
     */

    :root {
      --xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
      --xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
      --xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
      --xr-border-color: var(--jp-border-color2, #e0e0e0);
      --xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
      --xr-background-color: var(--jp-layout-color0, white);
      --xr-background-color-row-even: var(--jp-layout-color1, white);
      --xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
    }

    html[theme=dark],
    body[data-theme=dark],
    body.vscode-dark {
      --xr-font-color0: rgba(255, 255, 255, 1);
      --xr-font-color2: rgba(255, 255, 255, 0.54);
      --xr-font-color3: rgba(255, 255, 255, 0.38);
      --xr-border-color: #1F1F1F;
      --xr-disabled-color: #515151;
      --xr-background-color: #111111;
      --xr-background-color-row-even: #111111;
      --xr-background-color-row-odd: #313131;
    }

    .xr-wrap {
      display: block !important;
      min-width: 300px;
      max-width: 700px;
    }

    .xr-text-repr-fallback {
      /* fallback to plain text repr when CSS is not injected (untrusted notebook) */
      display: none;
    }

    .xr-header {
      padding-top: 6px;
      padding-bottom: 6px;
      margin-bottom: 4px;
      border-bottom: solid 1px var(--xr-border-color);
    }

    .xr-header > div,
    .xr-header > ul {
      display: inline;
      margin-top: 0;
      margin-bottom: 0;
    }

    .xr-obj-type,
    .xr-array-name {
      margin-left: 2px;
      margin-right: 10px;
    }

    .xr-obj-type {
      color: var(--xr-font-color2);
    }

    .xr-sections {
      padding-left: 0 !important;
      display: grid;
      grid-template-columns: 150px auto auto 1fr 20px 20px;
    }

    .xr-section-item {
      display: contents;
    }

    .xr-section-item input {
      display: none;
    }

    .xr-section-item input + label {
      color: var(--xr-disabled-color);
    }

    .xr-section-item input:enabled + label {
      cursor: pointer;
      color: var(--xr-font-color2);
    }

    .xr-section-item input:enabled + label:hover {
      color: var(--xr-font-color0);
    }

    .xr-section-summary {
      grid-column: 1;
      color: var(--xr-font-color2);
      font-weight: 500;
    }

    .xr-section-summary > span {
      display: inline-block;
      padding-left: 0.5em;
    }

    .xr-section-summary-in:disabled + label {
      color: var(--xr-font-color2);
    }

    .xr-section-summary-in + label:before {
      display: inline-block;
      content: '►';
      font-size: 11px;
      width: 15px;
      text-align: center;
    }

    .xr-section-summary-in:disabled + label:before {
      color: var(--xr-disabled-color);
    }

    .xr-section-summary-in:checked + label:before {
      content: '▼';
    }

    .xr-section-summary-in:checked + label > span {
      display: none;
    }

    .xr-section-summary,
    .xr-section-inline-details {
      padding-top: 4px;
      padding-bottom: 4px;
    }

    .xr-section-inline-details {
      grid-column: 2 / -1;
    }

    .xr-section-details {
      display: none;
      grid-column: 1 / -1;
      margin-bottom: 5px;
    }

    .xr-section-summary-in:checked ~ .xr-section-details {
      display: contents;
    }

    .xr-array-wrap {
      grid-column: 1 / -1;
      display: grid;
      grid-template-columns: 20px auto;
    }

    .xr-array-wrap > label {
      grid-column: 1;
      vertical-align: top;
    }

    .xr-preview {
      color: var(--xr-font-color3);
    }

    .xr-array-preview,
    .xr-array-data {
      padding: 0 5px !important;
      grid-column: 2;
    }

    .xr-array-data,
    .xr-array-in:checked ~ .xr-array-preview {
      display: none;
    }

    .xr-array-in:checked ~ .xr-array-data,
    .xr-array-preview {
      display: inline-block;
    }

    .xr-dim-list {
      display: inline-block !important;
      list-style: none;
      padding: 0 !important;
      margin: 0;
    }

    .xr-dim-list li {
      display: inline-block;
      padding: 0;
      margin: 0;
    }

    .xr-dim-list:before {
      content: '(';
    }

    .xr-dim-list:after {
      content: ')';
    }

    .xr-dim-list li:not(:last-child):after {
      content: ',';
      padding-right: 5px;
    }

    .xr-has-index {
      font-weight: bold;
    }

    .xr-var-list,
    .xr-var-item {
      display: contents;
    }

    .xr-var-item > div,
    .xr-var-item label,
    .xr-var-item > .xr-var-name span {
      background-color: var(--xr-background-color-row-even);
      margin-bottom: 0;
    }

    .xr-var-item > .xr-var-name:hover span {
      padding-right: 5px;
    }

    .xr-var-list > li:nth-child(odd) > div,
    .xr-var-list > li:nth-child(odd) > label,
    .xr-var-list > li:nth-child(odd) > .xr-var-name span {
      background-color: var(--xr-background-color-row-odd);
    }

    .xr-var-name {
      grid-column: 1;
    }

    .xr-var-dims {
      grid-column: 2;
    }

    .xr-var-dtype {
      grid-column: 3;
      text-align: right;
      color: var(--xr-font-color2);
    }

    .xr-var-preview {
      grid-column: 4;
    }

    .xr-index-preview {
      grid-column: 2 / 5;
      color: var(--xr-font-color2);
    }

    .xr-var-name,
    .xr-var-dims,
    .xr-var-dtype,
    .xr-preview,
    .xr-attrs dt {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      padding-right: 10px;
    }

    .xr-var-name:hover,
    .xr-var-dims:hover,
    .xr-var-dtype:hover,
    .xr-attrs dt:hover {
      overflow: visible;
      width: auto;
      z-index: 1;
    }

    .xr-var-attrs,
    .xr-var-data,
    .xr-index-data {
      display: none;
      background-color: var(--xr-background-color) !important;
      padding-bottom: 5px !important;
    }

    .xr-var-attrs-in:checked ~ .xr-var-attrs,
    .xr-var-data-in:checked ~ .xr-var-data,
    .xr-index-data-in:checked ~ .xr-index-data {
      display: block;
    }

    .xr-var-data > table {
      float: right;
    }

    .xr-var-name span,
    .xr-var-data,
    .xr-index-name div,
    .xr-index-data,
    .xr-attrs {
      padding-left: 25px !important;
    }

    .xr-attrs,
    .xr-var-attrs,
    .xr-var-data,
    .xr-index-data {
      grid-column: 1 / -1;
    }

    dl.xr-attrs {
      padding: 0;
      margin: 0;
      display: grid;
      grid-template-columns: 125px auto;
    }

    .xr-attrs dt,
    .xr-attrs dd {
      padding: 0;
      margin: 0;
      float: left;
      padding-right: 10px;
      width: auto;
    }

    .xr-attrs dt {
      font-weight: normal;
      grid-column: 1;
    }

    .xr-attrs dt:hover span {
      display: inline-block;
      background: var(--xr-background-color);
      padding-right: 10px;
    }

    .xr-attrs dd {
      grid-column: 2;
      white-space: pre-wrap;
      word-break: break-all;
    }

    .xr-icon-database,
    .xr-icon-file-text2,
    .xr-no-icon {
      display: inline-block;
      vertical-align: middle;
      width: 1em;
      height: 1.5em !important;
      stroke-width: 0;
      stroke: currentColor;
      fill: currentColor;
    }
    </style><pre class='xr-text-repr-fallback'>&lt;xarray.DataArray (element: 10, station: 768)&gt;
    43.61 58.73 58.14 43.98 60.9 54.0 82.72 ... 0.196 0.202 0.207 0.109 0.141 0.185
    Coordinates:
      * station  (station) object MultiIndex
      * x        (station) float64 -6.197e+05 2.147e+05 ... -2.82e+05 -1.273e+05
      * y        (station) float64 6.805e+06 7.746e+06 ... 5.796e+06 6.523e+06
      * element  (element) object &#x27;SiO2_T&#x27; &#x27;TiO2_T&#x27; &#x27;Al2O3_T&#x27; ... &#x27;K2O_T&#x27; &#x27;P2O5_T&#x27;</pre><div class='xr-wrap' style='display:none'><div class='xr-header'><div class='xr-obj-type'>xarray.DataArray</div><div class='xr-array-name'></div><ul class='xr-dim-list'><li><span class='xr-has-index'>element</span>: 10</li><li><span class='xr-has-index'>station</span>: 768</li></ul></div><ul class='xr-sections'><li class='xr-section-item'><div class='xr-array-wrap'><input id='section-69ee1fcc-e5e8-48d0-a082-64b5ebedfdcb' class='xr-array-in' type='checkbox' ><label for='section-69ee1fcc-e5e8-48d0-a082-64b5ebedfdcb' title='Show/hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-array-preview xr-preview'><span>43.61 58.73 58.14 43.98 60.9 54.0 ... 0.202 0.207 0.109 0.141 0.185</span></div><div class='xr-array-data'><pre>array([[43.61 , 58.73 , 58.14 , ..., 87.92 , 85.42 , 58.04 ],
           [ 1.29 ,  0.913,  0.902, ...,  0.386,  0.25 ,  0.61 ],
           [13.07 , 14.78 , 11.89 , ...,  4.74 ,  3.96 , 13.87 ],
           ...,
           [ 1.14 ,  2.19 ,  2.13 , ...,  0.53 ,  0.38 ,  1.63 ],
           [ 2.01 ,  1.78 ,  1.16 , ...,  1.39 ,  1.06 ,  3.11 ],
           [ 0.481,  0.298,  0.408, ...,  0.109,  0.141,  0.185]])</pre></div></div></li><li class='xr-section-item'><input id='section-fda19ade-ac5c-4797-ae0d-ec41a27f0760' class='xr-section-summary-in' type='checkbox'  checked><label for='section-fda19ade-ac5c-4797-ae0d-ec41a27f0760' class='xr-section-summary' >Coordinates: <span>(4)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>station</span></div><div class='xr-var-dims'>(station)</div><div class='xr-var-dtype'>object</div><div class='xr-var-preview xr-preview'>MultiIndex</div><input id='attrs-550e6822-9488-4e15-a107-36b18002dedb' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-550e6822-9488-4e15-a107-36b18002dedb' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-3009aed3-93f1-4221-ba31-184707984898' class='xr-var-data-in' type='checkbox'><label for='data-3009aed3-93f1-4221-ba31-184707984898' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([(-619656.5, 6805304.1), (214714.1, 7745546.6), (-368415.5, 7065039.2),
           ..., (-271994.2, 5840418.4), (-282020.2, 5795588.8),
           (-127326.9, 6523470.9)], dtype=object)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>x</span></div><div class='xr-var-dims'>(station)</div><div class='xr-var-dtype'>float64</div><div class='xr-var-preview xr-preview'>-6.197e+05 2.147e+05 ... -1.273e+05</div><input id='attrs-60fcdbc0-eb84-4f07-aa11-3efed1879e6c' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-60fcdbc0-eb84-4f07-aa11-3efed1879e6c' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-02c0d9f1-a00e-40bb-a76c-99543558aa19' class='xr-var-data-in' type='checkbox'><label for='data-02c0d9f1-a00e-40bb-a76c-99543558aa19' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([-619656.5,  214714.1, -368415.5, ..., -271994.2, -282020.2, -127326.9])</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>y</span></div><div class='xr-var-dims'>(station)</div><div class='xr-var-dtype'>float64</div><div class='xr-var-preview xr-preview'>6.805e+06 7.746e+06 ... 6.523e+06</div><input id='attrs-66a1fc18-d384-4f5f-9742-c8ceabb2d519' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-66a1fc18-d384-4f5f-9742-c8ceabb2d519' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-a768aae7-cc1d-4362-b274-2f0d46be6e57' class='xr-var-data-in' type='checkbox'><label for='data-a768aae7-cc1d-4362-b274-2f0d46be6e57' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([6805304.1, 7745546.6, 7065039.2, ..., 5840418.4, 5795588.8, 6523470.9])</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>element</span></div><div class='xr-var-dims'>(element)</div><div class='xr-var-dtype'>object</div><div class='xr-var-preview xr-preview'>&#x27;SiO2_T&#x27; &#x27;TiO2_T&#x27; ... &#x27;P2O5_T&#x27;</div><input id='attrs-61602114-1b28-4a4d-85d4-ba53fe946b30' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-61602114-1b28-4a4d-85d4-ba53fe946b30' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-ae2d90c6-4275-4de7-9516-6e1d79932aad' class='xr-var-data-in' type='checkbox'><label for='data-ae2d90c6-4275-4de7-9516-6e1d79932aad' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([&#x27;SiO2_T&#x27;, &#x27;TiO2_T&#x27;, &#x27;Al2O3_T&#x27;, &#x27;Fe2O3_T&#x27;, &#x27;MnO_T&#x27;, &#x27;MgO_T&#x27;, &#x27;CaO_T&#x27;,
           &#x27;Na2O_T&#x27;, &#x27;K2O_T&#x27;, &#x27;P2O5_T&#x27;], dtype=object)</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-a4387dcd-d30b-4af4-834c-05cd6922a5ea' class='xr-section-summary-in' type='checkbox'  ><label for='section-a4387dcd-d30b-4af4-834c-05cd6922a5ea' class='xr-section-summary' >Indexes: <span>(2)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-index-name'><div>station<br>x<br>y</div></div><div class='xr-index-preview'>PandasMultiIndex</div><div></div><input id='index-175d189a-9767-4e79-8ee2-ad67f3514d9f' class='xr-index-data-in' type='checkbox'/><label for='index-175d189a-9767-4e79-8ee2-ad67f3514d9f' title='Show/Hide index repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-index-data'><pre>PandasIndex(MultiIndex([(-619656.5, 6805304.1),
                ( 214714.1, 7745546.6),
                (-368415.5, 7065039.2),
                ( 226609.0, 6922431.0),
                ( 544050.0, 7808760.0),
                ( 379548.0, 6925430.0),
                ( 273624.6, 6312364.8),
                ( 177877.6, 6326128.4),
                (-391154.2, 6767421.3),
                ( 619052.0, 7120450.0),
                ...
                ( -26676.6, 5770677.8),
                (  56615.6, 5603218.7),
                ( 154093.5, 6031679.1),
                (-326852.3, 6614994.8),
                (-217166.1, 6289797.7),
                ( 121259.2, 7089496.9),
                (   -967.3, 6958100.4),
                (-271994.2, 5840418.4),
                (-282020.2, 5795588.8),
                (-127326.9, 6523470.9)],
               name=&#x27;station&#x27;, length=768))</pre></div></li><li class='xr-var-item'><div class='xr-index-name'><div>element</div></div><div class='xr-index-preview'>PandasIndex</div><div></div><input id='index-28900eac-3546-435d-be13-bf38bcdb4ad1' class='xr-index-data-in' type='checkbox'/><label for='index-28900eac-3546-435d-be13-bf38bcdb4ad1' title='Show/Hide index repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-index-data'><pre>PandasIndex(Index([&#x27;SiO2_T&#x27;, &#x27;TiO2_T&#x27;, &#x27;Al2O3_T&#x27;, &#x27;Fe2O3_T&#x27;, &#x27;MnO_T&#x27;, &#x27;MgO_T&#x27;, &#x27;CaO_T&#x27;,
           &#x27;Na2O_T&#x27;, &#x27;K2O_T&#x27;, &#x27;P2O5_T&#x27;],
          dtype=&#x27;object&#x27;, name=&#x27;element&#x27;))</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-c58b9894-d7a4-494c-a793-12d4ec80bb4d' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-c58b9894-d7a4-494c-a793-12d4ec80bb4d' class='xr-section-summary'  title='Expand/collapse section'>Attributes: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'></dl></div></li></ul></div></div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 87-98

Let's dive into the GWPCA. First, initialize a ``GWPCA`` instance and fit it to the data.
The ``station`` dimension serves as our sample dimension, along which the local PCAs will be applied.
Since these PCAs need to gauge distances to adjacent stations, we must specify
a distance metric. Our station data includes coordinates in meters, so we'll
choose the ``euclidean`` metric. If you have coordinates in degrees (like
latitude and longitude), choose the ``haversine`` metric instead.
We're also using a ``bisquare`` kernel with a bandwidth of 1000 km. Note that the
bandwidth unit always follows input data (which is in meters here),
except when using the ``haversine`` metric, which always gives distances in
kilometers. Lastly, we'll standardize the input to ensure consistent scales
for the chemical elements.

.. GENERATED FROM PYTHON SOURCE LINES 98-110

.. code-block:: default


    gwpca = xe.single.GWPCA(
        n_modes=5,
        standardize=True,
        metric="euclidean",
        kernel="bisquare",
        bandwidth=1000000.0,
    )
    gwpca.fit(da, "station")
    gwpca.components()







.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <div><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
    <defs>
    <symbol id="icon-database" viewBox="0 0 32 32">
    <path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
    <path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
    <path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
    </symbol>
    <symbol id="icon-file-text2" viewBox="0 0 32 32">
    <path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
    <path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
    <path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
    <path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
    </symbol>
    </defs>
    </svg>
    <style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
     *
     */

    :root {
      --xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
      --xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
      --xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
      --xr-border-color: var(--jp-border-color2, #e0e0e0);
      --xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
      --xr-background-color: var(--jp-layout-color0, white);
      --xr-background-color-row-even: var(--jp-layout-color1, white);
      --xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
    }

    html[theme=dark],
    body[data-theme=dark],
    body.vscode-dark {
      --xr-font-color0: rgba(255, 255, 255, 1);
      --xr-font-color2: rgba(255, 255, 255, 0.54);
      --xr-font-color3: rgba(255, 255, 255, 0.38);
      --xr-border-color: #1F1F1F;
      --xr-disabled-color: #515151;
      --xr-background-color: #111111;
      --xr-background-color-row-even: #111111;
      --xr-background-color-row-odd: #313131;
    }

    .xr-wrap {
      display: block !important;
      min-width: 300px;
      max-width: 700px;
    }

    .xr-text-repr-fallback {
      /* fallback to plain text repr when CSS is not injected (untrusted notebook) */
      display: none;
    }

    .xr-header {
      padding-top: 6px;
      padding-bottom: 6px;
      margin-bottom: 4px;
      border-bottom: solid 1px var(--xr-border-color);
    }

    .xr-header > div,
    .xr-header > ul {
      display: inline;
      margin-top: 0;
      margin-bottom: 0;
    }

    .xr-obj-type,
    .xr-array-name {
      margin-left: 2px;
      margin-right: 10px;
    }

    .xr-obj-type {
      color: var(--xr-font-color2);
    }

    .xr-sections {
      padding-left: 0 !important;
      display: grid;
      grid-template-columns: 150px auto auto 1fr 20px 20px;
    }

    .xr-section-item {
      display: contents;
    }

    .xr-section-item input {
      display: none;
    }

    .xr-section-item input + label {
      color: var(--xr-disabled-color);
    }

    .xr-section-item input:enabled + label {
      cursor: pointer;
      color: var(--xr-font-color2);
    }

    .xr-section-item input:enabled + label:hover {
      color: var(--xr-font-color0);
    }

    .xr-section-summary {
      grid-column: 1;
      color: var(--xr-font-color2);
      font-weight: 500;
    }

    .xr-section-summary > span {
      display: inline-block;
      padding-left: 0.5em;
    }

    .xr-section-summary-in:disabled + label {
      color: var(--xr-font-color2);
    }

    .xr-section-summary-in + label:before {
      display: inline-block;
      content: '►';
      font-size: 11px;
      width: 15px;
      text-align: center;
    }

    .xr-section-summary-in:disabled + label:before {
      color: var(--xr-disabled-color);
    }

    .xr-section-summary-in:checked + label:before {
      content: '▼';
    }

    .xr-section-summary-in:checked + label > span {
      display: none;
    }

    .xr-section-summary,
    .xr-section-inline-details {
      padding-top: 4px;
      padding-bottom: 4px;
    }

    .xr-section-inline-details {
      grid-column: 2 / -1;
    }

    .xr-section-details {
      display: none;
      grid-column: 1 / -1;
      margin-bottom: 5px;
    }

    .xr-section-summary-in:checked ~ .xr-section-details {
      display: contents;
    }

    .xr-array-wrap {
      grid-column: 1 / -1;
      display: grid;
      grid-template-columns: 20px auto;
    }

    .xr-array-wrap > label {
      grid-column: 1;
      vertical-align: top;
    }

    .xr-preview {
      color: var(--xr-font-color3);
    }

    .xr-array-preview,
    .xr-array-data {
      padding: 0 5px !important;
      grid-column: 2;
    }

    .xr-array-data,
    .xr-array-in:checked ~ .xr-array-preview {
      display: none;
    }

    .xr-array-in:checked ~ .xr-array-data,
    .xr-array-preview {
      display: inline-block;
    }

    .xr-dim-list {
      display: inline-block !important;
      list-style: none;
      padding: 0 !important;
      margin: 0;
    }

    .xr-dim-list li {
      display: inline-block;
      padding: 0;
      margin: 0;
    }

    .xr-dim-list:before {
      content: '(';
    }

    .xr-dim-list:after {
      content: ')';
    }

    .xr-dim-list li:not(:last-child):after {
      content: ',';
      padding-right: 5px;
    }

    .xr-has-index {
      font-weight: bold;
    }

    .xr-var-list,
    .xr-var-item {
      display: contents;
    }

    .xr-var-item > div,
    .xr-var-item label,
    .xr-var-item > .xr-var-name span {
      background-color: var(--xr-background-color-row-even);
      margin-bottom: 0;
    }

    .xr-var-item > .xr-var-name:hover span {
      padding-right: 5px;
    }

    .xr-var-list > li:nth-child(odd) > div,
    .xr-var-list > li:nth-child(odd) > label,
    .xr-var-list > li:nth-child(odd) > .xr-var-name span {
      background-color: var(--xr-background-color-row-odd);
    }

    .xr-var-name {
      grid-column: 1;
    }

    .xr-var-dims {
      grid-column: 2;
    }

    .xr-var-dtype {
      grid-column: 3;
      text-align: right;
      color: var(--xr-font-color2);
    }

    .xr-var-preview {
      grid-column: 4;
    }

    .xr-index-preview {
      grid-column: 2 / 5;
      color: var(--xr-font-color2);
    }

    .xr-var-name,
    .xr-var-dims,
    .xr-var-dtype,
    .xr-preview,
    .xr-attrs dt {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      padding-right: 10px;
    }

    .xr-var-name:hover,
    .xr-var-dims:hover,
    .xr-var-dtype:hover,
    .xr-attrs dt:hover {
      overflow: visible;
      width: auto;
      z-index: 1;
    }

    .xr-var-attrs,
    .xr-var-data,
    .xr-index-data {
      display: none;
      background-color: var(--xr-background-color) !important;
      padding-bottom: 5px !important;
    }

    .xr-var-attrs-in:checked ~ .xr-var-attrs,
    .xr-var-data-in:checked ~ .xr-var-data,
    .xr-index-data-in:checked ~ .xr-index-data {
      display: block;
    }

    .xr-var-data > table {
      float: right;
    }

    .xr-var-name span,
    .xr-var-data,
    .xr-index-name div,
    .xr-index-data,
    .xr-attrs {
      padding-left: 25px !important;
    }

    .xr-attrs,
    .xr-var-attrs,
    .xr-var-data,
    .xr-index-data {
      grid-column: 1 / -1;
    }

    dl.xr-attrs {
      padding: 0;
      margin: 0;
      display: grid;
      grid-template-columns: 125px auto;
    }

    .xr-attrs dt,
    .xr-attrs dd {
      padding: 0;
      margin: 0;
      float: left;
      padding-right: 10px;
      width: auto;
    }

    .xr-attrs dt {
      font-weight: normal;
      grid-column: 1;
    }

    .xr-attrs dt:hover span {
      display: inline-block;
      background: var(--xr-background-color);
      padding-right: 10px;
    }

    .xr-attrs dd {
      grid-column: 2;
      white-space: pre-wrap;
      word-break: break-all;
    }

    .xr-icon-database,
    .xr-icon-file-text2,
    .xr-no-icon {
      display: inline-block;
      vertical-align: middle;
      width: 1em;
      height: 1.5em !important;
      stroke-width: 0;
      stroke: currentColor;
      fill: currentColor;
    }
    </style><pre class='xr-text-repr-fallback'>&lt;xarray.DataArray &#x27;components&#x27; (mode: 5, element: 10, station: 768)&gt;
    0.1813 -0.3584 0.1243 0.2 -0.3812 ... 0.1229 -0.2865 0.4732 0.4197 -0.4249
    Coordinates:
      * mode     (mode) int64 1 2 3 4 5
      * element  (element) object &#x27;SiO2_T&#x27; &#x27;TiO2_T&#x27; &#x27;Al2O3_T&#x27; ... &#x27;K2O_T&#x27; &#x27;P2O5_T&#x27;
      * station  (station) object MultiIndex
      * x        (station) float64 -6.197e+05 2.147e+05 ... -2.82e+05 -1.273e+05
      * y        (station) float64 6.805e+06 7.746e+06 ... 5.796e+06 6.523e+06
    Attributes: (15)</pre><div class='xr-wrap' style='display:none'><div class='xr-header'><div class='xr-obj-type'>xarray.DataArray</div><div class='xr-array-name'>'components'</div><ul class='xr-dim-list'><li><span class='xr-has-index'>mode</span>: 5</li><li><span class='xr-has-index'>element</span>: 10</li><li><span class='xr-has-index'>station</span>: 768</li></ul></div><ul class='xr-sections'><li class='xr-section-item'><div class='xr-array-wrap'><input id='section-e7db8e19-49a2-47c1-a20a-43ca39ca97f7' class='xr-array-in' type='checkbox' ><label for='section-e7db8e19-49a2-47c1-a20a-43ca39ca97f7' title='Show/hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-array-preview xr-preview'><span>0.1813 -0.3584 0.1243 0.2 -0.3812 ... -0.2865 0.4732 0.4197 -0.4249</span></div><div class='xr-array-data'><pre>array([[[ 0.18125709, -0.3584175 ,  0.12428877, ...,  0.25671877,
              0.256246  ,  0.22973313],
            [-0.41417985, -0.39376878, -0.42011077, ..., -0.34732466,
             -0.36015219, -0.36847694],
            [-0.23899558, -0.40549381, -0.22096686, ..., -0.43839062,
             -0.43602713, -0.38387998],
            ...,
            [-0.14042662, -0.33330634, -0.1007027 , ..., -0.29087989,
             -0.28052648, -0.2727621 ],
            [ 0.02738883, -0.28666283,  0.07904099, ..., -0.45953578,
             -0.45518779, -0.23694713],
            [-0.34940508,  0.13105706, -0.29440609, ..., -0.1684628 ,
             -0.16530684, -0.24930107]],

           [[ 0.06454951, -0.3902977 ,  0.30726531, ..., -0.07248252,
             -0.06811968,  0.25577407],
            [-0.03386657,  0.14889056,  0.11770386, ..., -0.17060107,
             -0.17355007, -0.03515532],
            [ 0.39356621, -0.10344122,  0.32981395, ..., -0.11602203,
             -0.1215438 ,  0.29653649],
    ...
            [-0.44434099, -0.16170504,  0.57227612, ...,  0.43838271,
              0.43921397, -0.11513814],
            [ 0.05248914, -0.47102639,  0.17100995, ..., -0.25704447,
             -0.24795008, -0.03612217],
            [ 0.18281418, -0.77203888,  0.09900262, ...,  0.46462819,
              0.46644696, -0.41702666]],

           [[ 0.10642893,  0.1681947 ,  0.19064764, ..., -0.24733942,
             -0.23866297,  0.39431099],
            [-0.55691324, -0.34857872, -0.38967335, ..., -0.19591943,
             -0.23186739, -0.0380259 ],
            [ 0.02509266, -0.17664588, -0.03154743, ...,  0.13656387,
              0.16688651, -0.07318026],
            ...,
            [ 0.15740792,  0.3383997 ,  0.30399417, ..., -0.03244878,
             -0.05698807,  0.21161562],
            [-0.20769618, -0.24960301, -0.29431265, ..., -0.37464037,
             -0.36863136, -0.20368482],
            [-0.2285085 , -0.07289737, -0.26811733, ...,  0.47324595,
              0.41969128, -0.42492974]]])</pre></div></div></li><li class='xr-section-item'><input id='section-28133153-d38e-4639-972c-821a2bf8fa31' class='xr-section-summary-in' type='checkbox'  checked><label for='section-28133153-d38e-4639-972c-821a2bf8fa31' class='xr-section-summary' >Coordinates: <span>(5)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>mode</span></div><div class='xr-var-dims'>(mode)</div><div class='xr-var-dtype'>int64</div><div class='xr-var-preview xr-preview'>1 2 3 4 5</div><input id='attrs-f10807e4-a47f-4e38-82b3-e372147eeaea' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-f10807e4-a47f-4e38-82b3-e372147eeaea' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-4ad4a91d-52c7-436d-928a-e0287f6fb1f8' class='xr-var-data-in' type='checkbox'><label for='data-4ad4a91d-52c7-436d-928a-e0287f6fb1f8' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([1, 2, 3, 4, 5])</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>element</span></div><div class='xr-var-dims'>(element)</div><div class='xr-var-dtype'>object</div><div class='xr-var-preview xr-preview'>&#x27;SiO2_T&#x27; &#x27;TiO2_T&#x27; ... &#x27;P2O5_T&#x27;</div><input id='attrs-d87fa66e-5682-41e9-b937-147153aa05fc' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-d87fa66e-5682-41e9-b937-147153aa05fc' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-e38330d2-188c-4c95-bb6e-e128ad741fa4' class='xr-var-data-in' type='checkbox'><label for='data-e38330d2-188c-4c95-bb6e-e128ad741fa4' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([&#x27;SiO2_T&#x27;, &#x27;TiO2_T&#x27;, &#x27;Al2O3_T&#x27;, &#x27;Fe2O3_T&#x27;, &#x27;MnO_T&#x27;, &#x27;MgO_T&#x27;, &#x27;CaO_T&#x27;,
           &#x27;Na2O_T&#x27;, &#x27;K2O_T&#x27;, &#x27;P2O5_T&#x27;], dtype=object)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>station</span></div><div class='xr-var-dims'>(station)</div><div class='xr-var-dtype'>object</div><div class='xr-var-preview xr-preview'>MultiIndex</div><input id='attrs-b93c58ce-2106-4354-a95f-fb5ed2858b99' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-b93c58ce-2106-4354-a95f-fb5ed2858b99' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-d0fc3de5-390b-47be-96a0-a8f3275c22a4' class='xr-var-data-in' type='checkbox'><label for='data-d0fc3de5-390b-47be-96a0-a8f3275c22a4' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([(-619656.5, 6805304.1), (214714.1, 7745546.6), (-368415.5, 7065039.2),
           ..., (-271994.2, 5840418.4), (-282020.2, 5795588.8),
           (-127326.9, 6523470.9)], dtype=object)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>x</span></div><div class='xr-var-dims'>(station)</div><div class='xr-var-dtype'>float64</div><div class='xr-var-preview xr-preview'>-6.197e+05 2.147e+05 ... -1.273e+05</div><input id='attrs-bb34dbf1-65b2-4877-b3e6-1ef1c773765f' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-bb34dbf1-65b2-4877-b3e6-1ef1c773765f' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-99178bf5-37c5-4dd1-9610-dacb870fef0f' class='xr-var-data-in' type='checkbox'><label for='data-99178bf5-37c5-4dd1-9610-dacb870fef0f' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([-619656.5,  214714.1, -368415.5, ..., -271994.2, -282020.2, -127326.9])</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>y</span></div><div class='xr-var-dims'>(station)</div><div class='xr-var-dtype'>float64</div><div class='xr-var-preview xr-preview'>6.805e+06 7.746e+06 ... 6.523e+06</div><input id='attrs-58430055-5389-45eb-bc3b-54e95da7e19a' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-58430055-5389-45eb-bc3b-54e95da7e19a' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-60ac4a86-3d95-4637-a14c-9d0429420f20' class='xr-var-data-in' type='checkbox'><label for='data-60ac4a86-3d95-4637-a14c-9d0429420f20' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([6805304.1, 7745546.6, 7065039.2, ..., 5840418.4, 5795588.8, 6523470.9])</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-e71925bd-41c1-4f2e-941b-6abb020776a9' class='xr-section-summary-in' type='checkbox'  ><label for='section-e71925bd-41c1-4f2e-941b-6abb020776a9' class='xr-section-summary' >Indexes: <span>(3)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-index-name'><div>mode</div></div><div class='xr-index-preview'>PandasIndex</div><div></div><input id='index-4f82eeaa-f7a4-4b15-9aec-d683942843fb' class='xr-index-data-in' type='checkbox'/><label for='index-4f82eeaa-f7a4-4b15-9aec-d683942843fb' title='Show/Hide index repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-index-data'><pre>PandasIndex(Index([1, 2, 3, 4, 5], dtype=&#x27;int64&#x27;, name=&#x27;mode&#x27;))</pre></div></li><li class='xr-var-item'><div class='xr-index-name'><div>element</div></div><div class='xr-index-preview'>PandasIndex</div><div></div><input id='index-fa40b8f4-e541-4238-abf7-983aa1637421' class='xr-index-data-in' type='checkbox'/><label for='index-fa40b8f4-e541-4238-abf7-983aa1637421' title='Show/Hide index repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-index-data'><pre>PandasIndex(Index([&#x27;SiO2_T&#x27;, &#x27;TiO2_T&#x27;, &#x27;Al2O3_T&#x27;, &#x27;Fe2O3_T&#x27;, &#x27;MnO_T&#x27;, &#x27;MgO_T&#x27;, &#x27;CaO_T&#x27;,
           &#x27;Na2O_T&#x27;, &#x27;K2O_T&#x27;, &#x27;P2O5_T&#x27;],
          dtype=&#x27;object&#x27;, name=&#x27;element&#x27;))</pre></div></li><li class='xr-var-item'><div class='xr-index-name'><div>station<br>x<br>y</div></div><div class='xr-index-preview'>PandasMultiIndex</div><div></div><input id='index-f2fe91db-2b91-4d9a-ac45-f2c53bc8f004' class='xr-index-data-in' type='checkbox'/><label for='index-f2fe91db-2b91-4d9a-ac45-f2c53bc8f004' title='Show/Hide index repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-index-data'><pre>PandasIndex(MultiIndex([(-619656.5, 6805304.1),
                ( 214714.1, 7745546.6),
                (-368415.5, 7065039.2),
                ( 226609.0, 6922431.0),
                ( 544050.0, 7808760.0),
                ( 379548.0, 6925430.0),
                ( 273624.6, 6312364.8),
                ( 177877.6, 6326128.4),
                (-391154.2, 6767421.3),
                ( 619052.0, 7120450.0),
                ...
                ( -26676.6, 5770677.8),
                (  56615.6, 5603218.7),
                ( 154093.5, 6031679.1),
                (-326852.3, 6614994.8),
                (-217166.1, 6289797.7),
                ( 121259.2, 7089496.9),
                (   -967.3, 6958100.4),
                (-271994.2, 5840418.4),
                (-282020.2, 5795588.8),
                (-127326.9, 6523470.9)],
               name=&#x27;station&#x27;, length=768))</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-a6a76f44-80fb-4a6c-8144-33914721a31d' class='xr-section-summary-in' type='checkbox'  ><label for='section-a6a76f44-80fb-4a6c-8144-33914721a31d' class='xr-section-summary' >Attributes: <span>(15)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'><dt><span>model :</span></dt><dd>GWPCA</dd><dt><span>software :</span></dt><dd>xeofs</dd><dt><span>version :</span></dt><dd>1.2.0</dd><dt><span>date :</span></dt><dd>2024-09-04 15:06:08</dd><dt><span>n_modes :</span></dt><dd>5</dd><dt><span>center :</span></dt><dd>True</dd><dt><span>standardize :</span></dt><dd>True</dd><dt><span>use_coslat :</span></dt><dd>False</dd><dt><span>check_nans :</span></dt><dd>True</dd><dt><span>sample_name :</span></dt><dd>sample</dd><dt><span>feature_name :</span></dt><dd>feature</dd><dt><span>random_state :</span></dt><dd>None</dd><dt><span>compute :</span></dt><dd>True</dd><dt><span>solver :</span></dt><dd>auto</dd><dt><span>solver_kwargs :</span></dt><dd>{}</dd></dl></div></li></ul></div></div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 111-116

The ``components`` method returns the local principal components for each station. Note that the
dimensionality of the returned array is ``[station, element, mode]``, so in practice we don't really have
reduced the dimensionality of the data set. However, we can
extract the largest locally weighted components for each station which tells us which chemical elements
dominate the local PCAs.

.. GENERATED FROM PYTHON SOURCE LINES 116-120

.. code-block:: default


    llwc = gwpca.largest_locally_weighted_components()
    llwc






.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <div><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
    <defs>
    <symbol id="icon-database" viewBox="0 0 32 32">
    <path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
    <path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
    <path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
    </symbol>
    <symbol id="icon-file-text2" viewBox="0 0 32 32">
    <path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
    <path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
    <path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
    <path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
    </symbol>
    </defs>
    </svg>
    <style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
     *
     */

    :root {
      --xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
      --xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
      --xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
      --xr-border-color: var(--jp-border-color2, #e0e0e0);
      --xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
      --xr-background-color: var(--jp-layout-color0, white);
      --xr-background-color-row-even: var(--jp-layout-color1, white);
      --xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
    }

    html[theme=dark],
    body[data-theme=dark],
    body.vscode-dark {
      --xr-font-color0: rgba(255, 255, 255, 1);
      --xr-font-color2: rgba(255, 255, 255, 0.54);
      --xr-font-color3: rgba(255, 255, 255, 0.38);
      --xr-border-color: #1F1F1F;
      --xr-disabled-color: #515151;
      --xr-background-color: #111111;
      --xr-background-color-row-even: #111111;
      --xr-background-color-row-odd: #313131;
    }

    .xr-wrap {
      display: block !important;
      min-width: 300px;
      max-width: 700px;
    }

    .xr-text-repr-fallback {
      /* fallback to plain text repr when CSS is not injected (untrusted notebook) */
      display: none;
    }

    .xr-header {
      padding-top: 6px;
      padding-bottom: 6px;
      margin-bottom: 4px;
      border-bottom: solid 1px var(--xr-border-color);
    }

    .xr-header > div,
    .xr-header > ul {
      display: inline;
      margin-top: 0;
      margin-bottom: 0;
    }

    .xr-obj-type,
    .xr-array-name {
      margin-left: 2px;
      margin-right: 10px;
    }

    .xr-obj-type {
      color: var(--xr-font-color2);
    }

    .xr-sections {
      padding-left: 0 !important;
      display: grid;
      grid-template-columns: 150px auto auto 1fr 20px 20px;
    }

    .xr-section-item {
      display: contents;
    }

    .xr-section-item input {
      display: none;
    }

    .xr-section-item input + label {
      color: var(--xr-disabled-color);
    }

    .xr-section-item input:enabled + label {
      cursor: pointer;
      color: var(--xr-font-color2);
    }

    .xr-section-item input:enabled + label:hover {
      color: var(--xr-font-color0);
    }

    .xr-section-summary {
      grid-column: 1;
      color: var(--xr-font-color2);
      font-weight: 500;
    }

    .xr-section-summary > span {
      display: inline-block;
      padding-left: 0.5em;
    }

    .xr-section-summary-in:disabled + label {
      color: var(--xr-font-color2);
    }

    .xr-section-summary-in + label:before {
      display: inline-block;
      content: '►';
      font-size: 11px;
      width: 15px;
      text-align: center;
    }

    .xr-section-summary-in:disabled + label:before {
      color: var(--xr-disabled-color);
    }

    .xr-section-summary-in:checked + label:before {
      content: '▼';
    }

    .xr-section-summary-in:checked + label > span {
      display: none;
    }

    .xr-section-summary,
    .xr-section-inline-details {
      padding-top: 4px;
      padding-bottom: 4px;
    }

    .xr-section-inline-details {
      grid-column: 2 / -1;
    }

    .xr-section-details {
      display: none;
      grid-column: 1 / -1;
      margin-bottom: 5px;
    }

    .xr-section-summary-in:checked ~ .xr-section-details {
      display: contents;
    }

    .xr-array-wrap {
      grid-column: 1 / -1;
      display: grid;
      grid-template-columns: 20px auto;
    }

    .xr-array-wrap > label {
      grid-column: 1;
      vertical-align: top;
    }

    .xr-preview {
      color: var(--xr-font-color3);
    }

    .xr-array-preview,
    .xr-array-data {
      padding: 0 5px !important;
      grid-column: 2;
    }

    .xr-array-data,
    .xr-array-in:checked ~ .xr-array-preview {
      display: none;
    }

    .xr-array-in:checked ~ .xr-array-data,
    .xr-array-preview {
      display: inline-block;
    }

    .xr-dim-list {
      display: inline-block !important;
      list-style: none;
      padding: 0 !important;
      margin: 0;
    }

    .xr-dim-list li {
      display: inline-block;
      padding: 0;
      margin: 0;
    }

    .xr-dim-list:before {
      content: '(';
    }

    .xr-dim-list:after {
      content: ')';
    }

    .xr-dim-list li:not(:last-child):after {
      content: ',';
      padding-right: 5px;
    }

    .xr-has-index {
      font-weight: bold;
    }

    .xr-var-list,
    .xr-var-item {
      display: contents;
    }

    .xr-var-item > div,
    .xr-var-item label,
    .xr-var-item > .xr-var-name span {
      background-color: var(--xr-background-color-row-even);
      margin-bottom: 0;
    }

    .xr-var-item > .xr-var-name:hover span {
      padding-right: 5px;
    }

    .xr-var-list > li:nth-child(odd) > div,
    .xr-var-list > li:nth-child(odd) > label,
    .xr-var-list > li:nth-child(odd) > .xr-var-name span {
      background-color: var(--xr-background-color-row-odd);
    }

    .xr-var-name {
      grid-column: 1;
    }

    .xr-var-dims {
      grid-column: 2;
    }

    .xr-var-dtype {
      grid-column: 3;
      text-align: right;
      color: var(--xr-font-color2);
    }

    .xr-var-preview {
      grid-column: 4;
    }

    .xr-index-preview {
      grid-column: 2 / 5;
      color: var(--xr-font-color2);
    }

    .xr-var-name,
    .xr-var-dims,
    .xr-var-dtype,
    .xr-preview,
    .xr-attrs dt {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      padding-right: 10px;
    }

    .xr-var-name:hover,
    .xr-var-dims:hover,
    .xr-var-dtype:hover,
    .xr-attrs dt:hover {
      overflow: visible;
      width: auto;
      z-index: 1;
    }

    .xr-var-attrs,
    .xr-var-data,
    .xr-index-data {
      display: none;
      background-color: var(--xr-background-color) !important;
      padding-bottom: 5px !important;
    }

    .xr-var-attrs-in:checked ~ .xr-var-attrs,
    .xr-var-data-in:checked ~ .xr-var-data,
    .xr-index-data-in:checked ~ .xr-index-data {
      display: block;
    }

    .xr-var-data > table {
      float: right;
    }

    .xr-var-name span,
    .xr-var-data,
    .xr-index-name div,
    .xr-index-data,
    .xr-attrs {
      padding-left: 25px !important;
    }

    .xr-attrs,
    .xr-var-attrs,
    .xr-var-data,
    .xr-index-data {
      grid-column: 1 / -1;
    }

    dl.xr-attrs {
      padding: 0;
      margin: 0;
      display: grid;
      grid-template-columns: 125px auto;
    }

    .xr-attrs dt,
    .xr-attrs dd {
      padding: 0;
      margin: 0;
      float: left;
      padding-right: 10px;
      width: auto;
    }

    .xr-attrs dt {
      font-weight: normal;
      grid-column: 1;
    }

    .xr-attrs dt:hover span {
      display: inline-block;
      background: var(--xr-background-color);
      padding-right: 10px;
    }

    .xr-attrs dd {
      grid-column: 2;
      white-space: pre-wrap;
      word-break: break-all;
    }

    .xr-icon-database,
    .xr-icon-file-text2,
    .xr-no-icon {
      display: inline-block;
      vertical-align: middle;
      width: 1em;
      height: 1.5em !important;
      stroke-width: 0;
      stroke: currentColor;
      fill: currentColor;
    }
    </style><pre class='xr-text-repr-fallback'>&lt;xarray.DataArray &#x27;largest_locally_weighted_components&#x27; (mode: 5, station: 768)&gt;
    &#x27;MgO_T&#x27; &#x27;Al2O3_T&#x27; &#x27;MgO_T&#x27; &#x27;TiO2_T&#x27; ... &#x27;K2O_T&#x27; &#x27;Fe2O3_T&#x27; &#x27;Fe2O3_T&#x27; &#x27;CaO_T&#x27;
    Coordinates:
      * mode     (mode) int64 1 2 3 4 5
      * station  (station) object MultiIndex
      * x        (station) float64 -6.197e+05 2.147e+05 ... -2.82e+05 -1.273e+05
      * y        (station) float64 6.805e+06 7.746e+06 ... 5.796e+06 6.523e+06</pre><div class='xr-wrap' style='display:none'><div class='xr-header'><div class='xr-obj-type'>xarray.DataArray</div><div class='xr-array-name'>'largest_locally_weighted_components'</div><ul class='xr-dim-list'><li><span class='xr-has-index'>mode</span>: 5</li><li><span class='xr-has-index'>station</span>: 768</li></ul></div><ul class='xr-sections'><li class='xr-section-item'><div class='xr-array-wrap'><input id='section-331aacca-dbd3-4bae-8555-ff764499908f' class='xr-array-in' type='checkbox' ><label for='section-331aacca-dbd3-4bae-8555-ff764499908f' title='Show/hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-array-preview xr-preview'><span>&#x27;MgO_T&#x27; &#x27;Al2O3_T&#x27; &#x27;MgO_T&#x27; &#x27;TiO2_T&#x27; ... &#x27;Fe2O3_T&#x27; &#x27;Fe2O3_T&#x27; &#x27;CaO_T&#x27;</span></div><div class='xr-array-data'><pre>array([[&#x27;MgO_T&#x27;, &#x27;Al2O3_T&#x27;, &#x27;MgO_T&#x27;, ..., &#x27;K2O_T&#x27;, &#x27;K2O_T&#x27;, &#x27;Fe2O3_T&#x27;],
           [&#x27;K2O_T&#x27;, &#x27;K2O_T&#x27;, &#x27;K2O_T&#x27;, ..., &#x27;CaO_T&#x27;, &#x27;CaO_T&#x27;, &#x27;K2O_T&#x27;],
           [&#x27;P2O5_T&#x27;, &#x27;MnO_T&#x27;, &#x27;P2O5_T&#x27;, ..., &#x27;MnO_T&#x27;, &#x27;MnO_T&#x27;, &#x27;Na2O_T&#x27;],
           [&#x27;MnO_T&#x27;, &#x27;P2O5_T&#x27;, &#x27;Na2O_T&#x27;, ..., &#x27;MnO_T&#x27;, &#x27;MnO_T&#x27;, &#x27;MnO_T&#x27;],
           [&#x27;MnO_T&#x27;, &#x27;MnO_T&#x27;, &#x27;MnO_T&#x27;, ..., &#x27;Fe2O3_T&#x27;, &#x27;Fe2O3_T&#x27;, &#x27;CaO_T&#x27;]],
          dtype=object)</pre></div></div></li><li class='xr-section-item'><input id='section-3f0dffb7-6f74-492d-a6be-f33e2a97f5cd' class='xr-section-summary-in' type='checkbox'  checked><label for='section-3f0dffb7-6f74-492d-a6be-f33e2a97f5cd' class='xr-section-summary' >Coordinates: <span>(4)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>mode</span></div><div class='xr-var-dims'>(mode)</div><div class='xr-var-dtype'>int64</div><div class='xr-var-preview xr-preview'>1 2 3 4 5</div><input id='attrs-457d8930-4496-4ac1-9a38-031bb8e1d810' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-457d8930-4496-4ac1-9a38-031bb8e1d810' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-51e69ba4-93bc-4621-a496-16548d6bcf13' class='xr-var-data-in' type='checkbox'><label for='data-51e69ba4-93bc-4621-a496-16548d6bcf13' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([1, 2, 3, 4, 5])</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>station</span></div><div class='xr-var-dims'>(station)</div><div class='xr-var-dtype'>object</div><div class='xr-var-preview xr-preview'>MultiIndex</div><input id='attrs-0e57c547-1575-4b79-bd13-d99d763af22e' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-0e57c547-1575-4b79-bd13-d99d763af22e' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-66c1448b-7686-46c7-9c00-cf2776bbfe45' class='xr-var-data-in' type='checkbox'><label for='data-66c1448b-7686-46c7-9c00-cf2776bbfe45' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([(-619656.5, 6805304.1), (214714.1, 7745546.6), (-368415.5, 7065039.2),
           ..., (-271994.2, 5840418.4), (-282020.2, 5795588.8),
           (-127326.9, 6523470.9)], dtype=object)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>x</span></div><div class='xr-var-dims'>(station)</div><div class='xr-var-dtype'>float64</div><div class='xr-var-preview xr-preview'>-6.197e+05 2.147e+05 ... -1.273e+05</div><input id='attrs-8950ed53-ac4b-45fb-b712-98502b731053' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-8950ed53-ac4b-45fb-b712-98502b731053' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-5417ad80-207f-4161-8d8f-1bf7321b27b9' class='xr-var-data-in' type='checkbox'><label for='data-5417ad80-207f-4161-8d8f-1bf7321b27b9' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([-619656.5,  214714.1, -368415.5, ..., -271994.2, -282020.2, -127326.9])</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>y</span></div><div class='xr-var-dims'>(station)</div><div class='xr-var-dtype'>float64</div><div class='xr-var-preview xr-preview'>6.805e+06 7.746e+06 ... 6.523e+06</div><input id='attrs-d65b4b56-dd01-4451-880c-f738bac3a51d' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-d65b4b56-dd01-4451-880c-f738bac3a51d' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-6473776e-cdbe-4f89-b899-b7696b184954' class='xr-var-data-in' type='checkbox'><label for='data-6473776e-cdbe-4f89-b899-b7696b184954' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([6805304.1, 7745546.6, 7065039.2, ..., 5840418.4, 5795588.8, 6523470.9])</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-9cecb7c2-c170-400b-b333-aa9caa536a7b' class='xr-section-summary-in' type='checkbox'  ><label for='section-9cecb7c2-c170-400b-b333-aa9caa536a7b' class='xr-section-summary' >Indexes: <span>(2)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-index-name'><div>mode</div></div><div class='xr-index-preview'>PandasIndex</div><div></div><input id='index-7afffa32-763e-4745-a2d8-2bf7c8354b9d' class='xr-index-data-in' type='checkbox'/><label for='index-7afffa32-763e-4745-a2d8-2bf7c8354b9d' title='Show/Hide index repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-index-data'><pre>PandasIndex(Index([1, 2, 3, 4, 5], dtype=&#x27;int64&#x27;, name=&#x27;mode&#x27;))</pre></div></li><li class='xr-var-item'><div class='xr-index-name'><div>station<br>x<br>y</div></div><div class='xr-index-preview'>PandasMultiIndex</div><div></div><input id='index-e7985224-d03a-4c85-9613-4c4e0ade7355' class='xr-index-data-in' type='checkbox'/><label for='index-e7985224-d03a-4c85-9613-4c4e0ade7355' title='Show/Hide index repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-index-data'><pre>PandasIndex(MultiIndex([(-619656.5, 6805304.1),
                ( 214714.1, 7745546.6),
                (-368415.5, 7065039.2),
                ( 226609.0, 6922431.0),
                ( 544050.0, 7808760.0),
                ( 379548.0, 6925430.0),
                ( 273624.6, 6312364.8),
                ( 177877.6, 6326128.4),
                (-391154.2, 6767421.3),
                ( 619052.0, 7120450.0),
                ...
                ( -26676.6, 5770677.8),
                (  56615.6, 5603218.7),
                ( 154093.5, 6031679.1),
                (-326852.3, 6614994.8),
                (-217166.1, 6289797.7),
                ( 121259.2, 7089496.9),
                (   -967.3, 6958100.4),
                (-271994.2, 5840418.4),
                (-282020.2, 5795588.8),
                (-127326.9, 6523470.9)],
               name=&#x27;station&#x27;, length=768))</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-eafe6d6c-9927-4188-b3c1-6fbc34757b3d' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-eafe6d6c-9927-4188-b3c1-6fbc34757b3d' class='xr-section-summary'  title='Expand/collapse section'>Attributes: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'></dl></div></li></ul></div></div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 121-126

Let's visualize the spatial patterns of the chemical elements.
As the stations are positioned on a irregular grid, we'll transform the
``llwc`` ``DataArray`` into a ``pandas.DataFrame``. After that, we can easily visualize
it using the ``scatter`` method.
For demonstation, we'll concentrate on the first mode:

.. GENERATED FROM PYTHON SOURCE LINES 126-157

.. code-block:: default


    llwc1_df = llwc.sel(mode=1).to_dataframe()

    elements = da.element.values
    n_elements = len(elements)
    colors = np.arange(n_elements)
    col_dict = {el: col for el, col in zip(elements, colors)}

    llwc1_df["colors"] = llwc1_df["largest_locally_weighted_components"].map(col_dict)
    cmap = sns.color_palette("tab10", n_colors=n_elements, as_cmap=True)


    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111)
    background_df.plot.scatter(ax=ax, x="V1", y="V2", color=".3", marker=".", s=1)
    s = ax.scatter(
        x=llwc1_df["x"],
        y=llwc1_df["y"],
        c=llwc1_df["colors"],
        ec="w",
        s=40,
        cmap=cmap,
        vmin=-0.5,
        vmax=n_elements - 0.5,
    )
    cbar = fig.colorbar(mappable=s, ax=ax, label="Largest locally weighted component")
    cbar.set_ticks(colors)
    cbar.set_ticklabels(elements)
    ax.set_title("Largest locally weighted element", loc="left", weight=800)
    plt.show()




.. image-sg:: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_gwpca_001.png
   :alt: Largest locally weighted element
   :srcset: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_gwpca_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 158-163

In the final step, let's examine the explained variance. Like standard PCA,
this gives us insight into the variance explained by each mode. But with a
local PCA for every station, the explained variance varies spatially. Notably,
the first mode's explained variance differs across countries, ranging from
roughly 40% to 70%.

.. GENERATED FROM PYTHON SOURCE LINES 163-176

.. code-block:: default



    exp_var_ratio = gwpca.explained_variance_ratio()
    evr1_df = exp_var_ratio.sel(mode=1).to_dataframe()

    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111)
    background_df.plot.scatter(ax=ax, x="V1", y="V2", color=".3", marker=".", s=1)
    evr1_df.plot.scatter(
        ax=ax, x="x", y="y", c="explained_variance_ratio", vmin=0.4, vmax=0.7
    )
    ax.set_title("Fraction of locally explained variance", loc="left", weight=800)
    plt.show()



.. image-sg:: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_gwpca_002.png
   :alt: Fraction of locally explained variance
   :srcset: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_gwpca_002.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_content_user_guide_auto_examples_1single_plot_gwpca.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_gwpca.py <plot_gwpca.py>`

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

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


.. only:: html

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

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