

.. _sphx_glr_gallery_api_compound_path.py:


=============
Compound path
=============

Make a compound path -- in this case two simple polygons, a rectangle
and a triangle.  Use CLOSEPOLY and MOVETO for the different parts of
the compound path




.. image:: /gallery/api/images/sphx_glr_compound_path_001.png
    :align: center





.. code-block:: python

    import numpy as np
    from matplotlib.path import Path
    from matplotlib.patches import PathPatch
    import matplotlib.pyplot as plt


    vertices = []
    codes = []

    codes = [Path.MOVETO] + [Path.LINETO]*3 + [Path.CLOSEPOLY]
    vertices = [(1, 1), (1, 2), (2, 2), (2, 1), (0, 0)]

    codes += [Path.MOVETO] + [Path.LINETO]*2 + [Path.CLOSEPOLY]
    vertices += [(4, 4), (5, 5), (5, 4), (0, 0)]

    vertices = np.array(vertices, float)
    path = Path(vertices, codes)

    pathpatch = PathPatch(path, facecolor='None', edgecolor='green')

    fig, ax = plt.subplots()
    ax.add_patch(pathpatch)
    ax.set_title('A compound path')

    ax.dataLim.update_from_data_xy(vertices)
    ax.autoscale_view()


    plt.show()

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



.. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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

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

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