.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/calculations/Gradient.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_Gradient.py: ======== Gradient ======== Use `metpy.calc.gradient`. This example demonstrates the various ways that MetPy's gradient function can be utilized. .. GENERATED FROM PYTHON SOURCE LINES 14-20 .. code-block:: Python import numpy as np import metpy.calc as mpcalc from metpy.units import units .. GENERATED FROM PYTHON SOURCE LINES 21-22 Create some test data to use for our example .. GENERATED FROM PYTHON SOURCE LINES 22-38 .. code-block:: Python data = np.array([[23, 24, 23], [25, 26, 25], [27, 28, 27], [24, 25, 24]]) * units.degC # Create an array of x position data (the coordinates of our temperature data) x = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]) * units.kilometer y = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]) * units.kilometer .. GENERATED FROM PYTHON SOURCE LINES 39-40 Calculate the gradient using the coordinates of the data .. GENERATED FROM PYTHON SOURCE LINES 40-44 .. code-block:: Python grad = mpcalc.gradient(data, coordinates=(y, x)) print('Gradient in y direction: ', grad[0]) print('Gradient in x direction: ', grad[1]) .. rst-class:: sphx-glr-script-out .. code-block:: none Gradient in y direction: [[2.0 2.0 2.0] [2.0 2.0 2.0] [-0.5 -0.5 -0.5] [-5.5 -5.5 -5.5]] kelvin / kilometer Gradient in x direction: [[2.0 0.0 -2.0] [2.0 0.0 -2.0] [2.0 0.0 -2.0] [2.0 0.0 -2.0]] kelvin / kilometer .. GENERATED FROM PYTHON SOURCE LINES 45-48 It's also possible that we do not have the position of data points, but know that they are evenly spaced. We can then specify a scalar delta value for each axes. .. GENERATED FROM PYTHON SOURCE LINES 48-54 .. code-block:: Python x_delta = 2 * units.km y_delta = 1 * units.km grad = mpcalc.gradient(data, deltas=(y_delta, x_delta)) print('Gradient in y direction: ', grad[0]) print('Gradient in x direction: ', grad[1]) .. rst-class:: sphx-glr-script-out .. code-block:: none Gradient in y direction: [[2.0 2.0 2.0] [2.0 2.0 2.0] [-0.5 -0.5 -0.5] [-5.5 -5.5 -5.5]] kelvin / kilometer Gradient in x direction: [[1.0 0.0 -1.0] [1.0 0.0 -1.0] [1.0 0.0 -1.0] [1.0 0.0 -1.0]] kelvin / kilometer .. GENERATED FROM PYTHON SOURCE LINES 55-56 Finally, the deltas can be arrays for unevenly spaced data. .. GENERATED FROM PYTHON SOURCE LINES 56-66 .. code-block:: Python x_deltas = np.array([[2, 3], [1, 3], [2, 3], [1, 2]]) * units.kilometer y_deltas = np.array([[2, 3, 1], [1, 3, 2], [2, 3, 1]]) * units.kilometer grad = mpcalc.gradient(data, deltas=(y_deltas, x_deltas)) print('Gradient in y direction: ', grad[0]) print('Gradient in x direction: ', grad[1]) .. rst-class:: sphx-glr-script-out .. code-block:: none Gradient in y direction: [[0.3333333333333428 0.6666666666666572 2.333333333333357] [1.6666666666666572 0.6666666666666714 1.6666666666666714] [0.8333333333333286 -0.1666666666666643 -1.6666666666666856] [-3.8333333333333144 -1.8333333333333428 -4.333333333333371]] kelvin / kilometer Gradient in x direction: [[0.833333333333357 0.1666666666666714 -0.8333333333333712] [1.333333333333286 0.6666666666666572 -1.3333333333333144] [0.833333333333357 0.1666666666666714 -0.8333333333333428] [1.5000000000000426 0.5000000000000142 -1.5]] kelvin / kilometer .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.015 seconds) .. _sphx_glr_download_examples_calculations_Gradient.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: Gradient.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: Gradient.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_