.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_convex_hull.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_auto_examples_plot_convex_hull.py: =========== Convex Hull =========== Convex hull of a set of points, representing Dom x-y positions. Derived from ``scipy.spatial.qhull.pyx``. .. GENERATED FROM PYTHON SOURCE LINES 12-32 .. code-block:: Python import numpy as np from scipy.spatial import ConvexHull import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # noqa from km3net_testdata import data_path from km3pipe.hardware import Detector from km3pipe.math import Polygon import km3pipe.style km3pipe.style.use("km3pipe") detx = data_path( "detx/orca_115strings_av23min20mhorizontal_18OMs_alt9mvertical_v1.detx" ) detector = Detector(detx) xy = detector.xy_positions hull = ConvexHull(xy) .. rst-class:: sphx-glr-script-out .. code-block:: none Detector: Parsing the DETX header Detector: Reading PMT information... Detector: Done. .. GENERATED FROM PYTHON SOURCE LINES 33-34 Plot it: .. GENERATED FROM PYTHON SOURCE LINES 34-39 .. code-block:: Python plt.plot(xy[:, 0], xy[:, 1], "o") for simplex in hull.simplices: plt.plot(xy[simplex, 0], xy[simplex, 1], "k-") .. image-sg:: /auto_examples/images/sphx_glr_plot_convex_hull_001.png :alt: plot convex hull :srcset: /auto_examples/images/sphx_glr_plot_convex_hull_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 40-42 We could also have directly used the vertices of the hull, which for 2-D are guaranteed to be in counterclockwise order: .. GENERATED FROM PYTHON SOURCE LINES 42-47 .. code-block:: Python plt.plot(xy[hull.vertices, 0], xy[hull.vertices, 1], "r--", lw=2) plt.plot(xy[hull.vertices[0], 0], xy[hull.vertices[0], 1], "ro") plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_convex_hull_002.png :alt: plot convex hull :srcset: /auto_examples/images/sphx_glr_plot_convex_hull_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 48-49 Now let's draw a polygon inside, and see which points are contained. .. GENERATED FROM PYTHON SOURCE LINES 49-65 .. code-block:: Python poly_vertices = np.array( [ (-60, 120), (80, 120), (110, 60), (110, -30), (70, -110), (-70, -110), (-90, -70), (-90, 60), ] ) poly = Polygon(poly_vertices) contain_mask = poly.contains(xy) .. GENERATED FROM PYTHON SOURCE LINES 66-67 and color them accordingly .. GENERATED FROM PYTHON SOURCE LINES 67-74 .. code-block:: Python plt.clf() plt.plot(xy[contain_mask, 0], xy[contain_mask, 1], "yo") plt.plot(xy[~contain_mask, 0], xy[~contain_mask, 1], "bo") plt.plot(poly_vertices[:, 0], poly_vertices[:, 1], "k-") plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_convex_hull_003.png :alt: plot convex hull :srcset: /auto_examples/images/sphx_glr_plot_convex_hull_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 75-76 plot the same in 3D because why not? .. GENERATED FROM PYTHON SOURCE LINES 76-86 .. code-block:: Python plt.clf() fig = plt.figure() ax = fig.add_subplot(111, projection="3d") ax.scatter(xy[:, 0], xy[:, 1], 90, "yo") ax.scatter(xy[:, 0], xy[:, 1], -90, "bo") ax.plot(poly_vertices[:, 0], poly_vertices[:, 1], 90, "k-") for simplex in hull.simplices: ax.plot(xy[simplex, 0], xy[simplex, 1], -90, "k-") plt.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_convex_hull_004.png :alt: plot convex hull :srcset: /auto_examples/images/sphx_glr_plot_convex_hull_004.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_convex_hull_005.png :alt: plot convex hull :srcset: /auto_examples/images/sphx_glr_plot_convex_hull_005.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.694 seconds) .. _sphx_glr_download_auto_examples_plot_convex_hull.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_convex_hull.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_convex_hull.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_