.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/trends.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_trends.py: Trend Estimation ================ Trend estimation and removal is a common operation, particularly when dealing with geophysical data. Moreover, some of the interpolation methods, like :class:`verde.Spline`, can struggle with long-wavelength trends in the data. The :class:`verde.Trend` class fits a 2D polynomial trend of arbitrary degree to the data and can be used to remove it. .. GENERATED FROM PYTHON SOURCE LINES 17-23 .. code-block:: default import cartopy.crs as ccrs import matplotlib.pyplot as plt import numpy as np import verde as vd .. GENERATED FROM PYTHON SOURCE LINES 24-26 Our sample air temperature data from Texas has a clear trend from land to the ocean: .. GENERATED FROM PYTHON SOURCE LINES 26-44 .. code-block:: default data = vd.datasets.fetch_texas_wind() coordinates = (data.longitude, data.latitude) plt.figure(figsize=(8, 6)) ax = plt.axes(projection=ccrs.Mercator()) plt.scatter( data.longitude, data.latitude, c=data.air_temperature_c, s=100, cmap="plasma", transform=ccrs.PlateCarree(), ) plt.colorbar().set_label("Air temperature (C)") vd.datasets.setup_texas_wind_map(ax) plt.show() .. image-sg:: /tutorials/images/sphx_glr_trends_001.png :alt: trends :srcset: /tutorials/images/sphx_glr_trends_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 45-46 We can estimate the polynomial coefficients for this trend: .. GENERATED FROM PYTHON SOURCE LINES 46-50 .. code-block:: default trend = vd.Trend(degree=1).fit(coordinates, data.air_temperature_c) print(trend.coef_) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [102.4946959 0.44373823 -1.48922224] .. GENERATED FROM PYTHON SOURCE LINES 51-53 More importantly, we can predict the trend values and remove them from our data: .. GENERATED FROM PYTHON SOURCE LINES 53-91 .. code-block:: default trend_values = trend.predict(coordinates) residuals = data.air_temperature_c - trend_values fig, axes = plt.subplots( 1, 2, figsize=(10, 6), subplot_kw=dict(projection=ccrs.Mercator()) ) ax = axes[0] ax.set_title("Trend") tmp = ax.scatter( data.longitude, data.latitude, c=trend_values, s=60, cmap="plasma", transform=ccrs.PlateCarree(), ) plt.colorbar(tmp, ax=ax, orientation="horizontal", pad=0.06) vd.datasets.setup_texas_wind_map(ax) ax = axes[1] ax.set_title("Residuals") maxabs = vd.maxabs(residuals) tmp = ax.scatter( data.longitude, data.latitude, c=residuals, s=60, cmap="bwr", vmin=-maxabs, vmax=maxabs, transform=ccrs.PlateCarree(), ) plt.colorbar(tmp, ax=ax, orientation="horizontal", pad=0.08) vd.datasets.setup_texas_wind_map(ax) plt.show() .. image-sg:: /tutorials/images/sphx_glr_trends_002.png :alt: Trend, Residuals :srcset: /tutorials/images/sphx_glr_trends_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 92-94 The fitting, prediction, and residual calculation can all be done in a single step using the :meth:`~verde.Trend.filter` method: .. GENERATED FROM PYTHON SOURCE LINES 94-101 .. code-block:: default # ``filter`` always outputs coordinates and weights as well, which we don't # need and will ignore here. __, res_filter, __ = vd.Trend(degree=1).filter(coordinates, data.air_temperature_c) print(np.allclose(res_filter, residuals)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 102-105 Additionally, :class:`verde.Trend` implements the :ref:`gridder interface ` and has the :meth:`~verde.Trend.grid` and :meth:`~verde.Trend.profile` methods. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.409 seconds) .. _sphx_glr_download_tutorials_trends.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: trends.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: trends.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_