.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/calculations/Smoothing.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_examples_calculations_Smoothing.py: ========= Smoothing ========= Using MetPy's smoothing functions. This example demonstrates the various ways that MetPy's smoothing function can be utilized. While this example utilizes basic NumPy arrays, these functions all work equally well with Pint Quantities or xarray DataArrays. .. GENERATED FROM PYTHON SOURCE LINES 15-23 .. code-block:: Python from itertools import product import matplotlib.pyplot as plt import numpy as np import metpy.calc as mpcalc .. GENERATED FROM PYTHON SOURCE LINES 24-25 Start with a base pattern with random noise .. GENERATED FROM PYTHON SOURCE LINES 25-37 .. code-block:: Python np.random.seed(61461542) size = 128 x, y = np.mgrid[:size, :size] distance = np.sqrt((x - size / 2) ** 2 + (y - size / 2) ** 2) raw_data = np.random.random((size, size)) * 0.3 + distance / distance.max() * 0.7 fig, ax = plt.subplots(1, 1, figsize=(4, 4)) ax.set_title('Raw Data') ax.imshow(raw_data, vmin=0, vmax=1) ax.axis('off') plt.show() .. image-sg:: /examples/calculations/images/sphx_glr_Smoothing_001.png :alt: Raw Data :srcset: /examples/calculations/images/sphx_glr_Smoothing_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 38-39 Now, create a grid showing different smoothing options .. GENERATED FROM PYTHON SOURCE LINES 39-77 .. code-block:: Python fig, ax = plt.subplots(3, 3, figsize=(12, 12)) for i, j in product(range(3), range(3)): ax[i, j].axis('off') # Gaussian Smoother ax[0, 0].imshow(mpcalc.smooth_gaussian(raw_data, 3), vmin=0, vmax=1) ax[0, 0].set_title('Gaussian - Low Degree') ax[0, 1].imshow(mpcalc.smooth_gaussian(raw_data, 8), vmin=0, vmax=1) ax[0, 1].set_title('Gaussian - High Degree') # Rectangular Smoother ax[0, 2].imshow(mpcalc.smooth_rectangular(raw_data, (3, 7), 2), vmin=0, vmax=1) ax[0, 2].set_title('Rectangular - 3x7 Window\n2 Passes') # 5-point smoother ax[1, 0].imshow(mpcalc.smooth_n_point(raw_data, 5, 1), vmin=0, vmax=1) ax[1, 0].set_title('5-Point - 1 Pass') ax[1, 1].imshow(mpcalc.smooth_n_point(raw_data, 5, 4), vmin=0, vmax=1) ax[1, 1].set_title('5-Point - 4 Passes') # Circular Smoother ax[1, 2].imshow(mpcalc.smooth_circular(raw_data, 2, 2), vmin=0, vmax=1) ax[1, 2].set_title('Circular - Radius 2\n2 Passes') # 9-point smoother ax[2, 0].imshow(mpcalc.smooth_n_point(raw_data, 9, 1), vmin=0, vmax=1) ax[2, 0].set_title('9-Point - 1 Pass') ax[2, 1].imshow(mpcalc.smooth_n_point(raw_data, 9, 4), vmin=0, vmax=1) ax[2, 1].set_title('9-Point - 4 Passes') # Arbitrary Window Smoother ax[2, 2].imshow(mpcalc.smooth_window(raw_data, np.diag(np.ones(5)), 2), vmin=0, vmax=1) ax[2, 2].set_title('Custom Window (Diagonal) \n2 Passes') plt.show() .. image-sg:: /examples/calculations/images/sphx_glr_Smoothing_002.png :alt: Gaussian - Low Degree, Gaussian - High Degree, Rectangular - 3x7 Window 2 Passes, 5-Point - 1 Pass, 5-Point - 4 Passes, Circular - Radius 2 2 Passes, 9-Point - 1 Pass, 9-Point - 4 Passes, Custom Window (Diagonal) 2 Passes :srcset: /examples/calculations/images/sphx_glr_Smoothing_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.513 seconds) .. _sphx_glr_download_examples_calculations_Smoothing.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: Smoothing.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: Smoothing.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_