.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "python_module/14_generic_tools/pet_fit_contour.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. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_python_module_14_generic_tools_pet_fit_contour.py: Contour fit =========== Two type of fit : - Ellipse - Circle In the two case we use a least square algorithm .. GENERATED FROM PYTHON SOURCE LINES 11-20 .. code-block:: Python from matplotlib import pyplot as plt from numpy import cos, linspace, radians, sin from py_eddy_tracker import data from py_eddy_tracker.generic import coordinates_to_local, local_to_coordinates from py_eddy_tracker.observations.observation import EddiesObservations from py_eddy_tracker.poly import fit_circle_, fit_ellipse .. GENERATED FROM PYTHON SOURCE LINES 21-22 Load example identification file .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. code-block:: Python a = EddiesObservations.load_file(data.get_demo_path("Anticyclonic_20190223.nc")) .. GENERATED FROM PYTHON SOURCE LINES 26-27 Function to draw circle or ellipse from parameter .. GENERATED FROM PYTHON SOURCE LINES 27-40 .. code-block:: Python def build_circle(x0, y0, r): angle = radians(linspace(0, 360, 50)) x_norm, y_norm = cos(angle), sin(angle) return local_to_coordinates(x_norm * r, y_norm * r, x0, y0) def build_ellipse(x0, y0, a, b, theta): angle = radians(linspace(0, 360, 50)) x = a * cos(theta) * cos(angle) - b * sin(theta) * sin(angle) y = a * sin(theta) * cos(angle) + b * cos(theta) * sin(angle) return local_to_coordinates(x, y, x0, y0) .. GENERATED FROM PYTHON SOURCE LINES 41-42 Plot fitted circle or ellipse on stored contour .. GENERATED FROM PYTHON SOURCE LINES 42-63 .. code-block:: Python xs, ys = a.contour_lon_s, a.contour_lat_s fig = plt.figure(figsize=(15, 15)) j = 1 for i in range(0, 800, 30): x, y = xs[i], ys[i] x0_, y0_ = x.mean(), y.mean() x_, y_ = coordinates_to_local(x, y, x0_, y0_) ax = fig.add_subplot(4, 4, j) ax.grid(), ax.set_aspect("equal") ax.plot(x, y, label="store", color="black") x0, y0, a, b, theta = fit_ellipse(x_, y_) x0, y0 = local_to_coordinates(x0, y0, x0_, y0_) ax.plot(*build_ellipse(x0, y0, a, b, theta), label="ellipse", color="green") x0, y0, radius, shape_error = fit_circle_(x_, y_) x0, y0 = local_to_coordinates(x0, y0, x0_, y0_) ax.plot(*build_circle(x0, y0, radius), label="circle", color="red", lw=0.5) if j == 16: break j += 1 .. image-sg:: /python_module/14_generic_tools/images/sphx_glr_pet_fit_contour_001.png :alt: pet fit contour :srcset: /python_module/14_generic_tools/images/sphx_glr_pet_fit_contour_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.170 seconds) .. _sphx_glr_download_python_module_14_generic_tools_pet_fit_contour.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/AntSimi/py-eddy-tracker/master?urlpath=lab/tree/notebooks/python_module/14_generic_tools/pet_fit_contour.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: pet_fit_contour.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: pet_fit_contour.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: pet_fit_contour.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_