

.. _sphx_glr_auto_examples_plot_convex_hull.py:


===========
Convex Hull
===========

Convex hull of a random set of points.

Lifted from ``scipy.spatial.qhull.pyx``.



.. code-block:: python


    import numpy as np
    from scipy.spatial import ConvexHull
    import matplotlib.pyplot as plt

    import km3pipe.style
    km3pipe.style.use("km3pipe")

    points = np.random.rand(30, 2)   # 30 random points in 2-D
    hull = ConvexHull(points)





.. rst-class:: sphx-glr-script-out

 Out::

    Loading style definitions from '/home/moritz/pkg/km3pipe/km3pipe/kp-data/stylelib/km3pipe.mplstyle'


Plot it:



.. code-block:: python


    plt.plot(points[:,0], points[:,1], 'o')
    for simplex in hull.simplices:
        plt.plot(points[simplex, 0], points[simplex, 1], 'k-')




.. image:: /auto_examples/images/sphx_glr_plot_convex_hull_001.png
    :align: center




We could also have directly used the vertices of the hull, which
for 2-D are guaranteed to be in counterclockwise order:



.. code-block:: python


    plt.plot(points[hull.vertices,0], points[hull.vertices,1], 'r--', lw=2)
    plt.plot(points[hull.vertices[0],0], points[hull.vertices[0],1], 'ro')
    plt.show()



.. image:: /auto_examples/images/sphx_glr_plot_convex_hull_002.png
    :align: center




**Total running time of the script:** ( 0 minutes  0.172 seconds)



.. container:: sphx-glr-footer


  .. container:: sphx-glr-download

     :download:`Download Python source code: plot_convex_hull.py <plot_convex_hull.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: plot_convex_hull.ipynb <plot_convex_hull.ipynb>`

.. rst-class:: sphx-glr-signature

    `Generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
