# xarray.corr

### xarray.corr(da_a, da_b, dim=None, weights=None)[[source]](https://github.com/pydata/xarray/blob/main/xarray/computation/computation.py#L147-L247)

Compute the Pearson correlation coefficient between
two DataArray objects along a shared dimension.

* **Parameters:**
  * **da_a** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray)) – Array to compute.
  * **da_b** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray)) – Array to compute.
  * **dim** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [iterable](https://docs.python.org/3/glossary.html#term-iterable) of [hashable](../user-guide/terminology.html.md#term-name), `"..."` or [`None`](https://docs.python.org/3/builtins/constants.html#None), *optional*) – The dimension along which the correlation will be computed
  * **weights** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray), *optional*) – Array of weights.
* **Returns:**
  **correlation** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray))

#### SEE ALSO
[`pandas.Series.corr`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.corr.html#pandas.Series.corr)
: corresponding pandas function

[`xarray.cov`](xarray.cov.html.md#xarray.cov)
: underlying covariance function

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('c5096f39-10a5-48a8-ae8e-fcbdf056f867','fd4f8df2-0582-4ca8-a345-f9926ef862ba','e637c9c2-0efa-4050-9f1f-8055c5b734d4','../lite/tree/../notebooks/index.html?path=e9acbdb3_c92b_409c_b0b4_fd4f5f99c340.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> from xarray import DataArray
>>> da_a = DataArray(
...     np.array([[1, 2, 3], [0.1, 0.2, 0.3], [3.2, 0.6, 1.8]]),
...     dims=("space", "time"),
...     coords=[
...         ("space", ["IA", "IL", "IN"]),
...         ("time", pd.date_range("2000-01-01", freq="1D", periods=3)),
...     ],
... )
>>> da_a
<xarray.DataArray (space: 3, time: 3)> Size: 72B
array([[1. , 2. , 3. ],
       [0.1, 0.2, 0.3],
       [3.2, 0.6, 1.8]])
Coordinates:
  * space    (space) <U2 24B 'IA' 'IL' 'IN'
  * time     (time) datetime64[us] 24B 2000-01-01 2000-01-02 2000-01-03
>>> da_b = DataArray(
...     np.array([[0.2, 0.4, 0.6], [15, 10, 5], [3.2, 0.6, 1.8]]),
...     dims=("space", "time"),
...     coords=[
...         ("space", ["IA", "IL", "IN"]),
...         ("time", pd.date_range("2000-01-01", freq="1D", periods=3)),
...     ],
... )
>>> da_b
<xarray.DataArray (space: 3, time: 3)> Size: 72B
array([[ 0.2,  0.4,  0.6],
       [15. , 10. ,  5. ],
       [ 3.2,  0.6,  1.8]])
Coordinates:
  * space    (space) <U2 24B 'IA' 'IL' 'IN'
  * time     (time) datetime64[us] 24B 2000-01-01 2000-01-02 2000-01-03
>>> xr.corr(da_a, da_b)
<xarray.DataArray ()> Size: 8B
array(-0.57087777)
>>> xr.corr(da_a, da_b, dim="time")
<xarray.DataArray (space: 3)> Size: 24B
array([ 1., -1.,  1.])
Coordinates:
  * space    (space) <U2 24B 'IA' 'IL' 'IN'
>>> weights = DataArray(
...     [4, 2, 1],
...     dims=("space"),
...     coords=[
...         ("space", ["IA", "IL", "IN"]),
...     ],
... )
>>> weights
<xarray.DataArray (space: 3)> Size: 24B
array([4, 2, 1])
Coordinates:
  * space    (space) <U2 24B 'IA' 'IL' 'IN'
>>> xr.corr(da_a, da_b, dim="space", weights=weights)
<xarray.DataArray (time: 3)> Size: 24B
array([-0.50240504, -0.83215028, -0.99057446])
Coordinates:
  * time     (time) datetime64[us] 24B 2000-01-01 2000-01-02 2000-01-03
```

<div id="e637c9c2-0efa-4050-9f1f-8055c5b734d4" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('c5096f39-10a5-48a8-ae8e-fcbdf056f867','e637c9c2-0efa-4050-9f1f-8055c5b734d4')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('c5096f39-10a5-48a8-ae8e-fcbdf056f867','e637c9c2-0efa-4050-9f1f-8055c5b734d4')">Open In Tab</button></div><div id="fd4f8df2-0582-4ca8-a345-f9926ef862ba" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
