
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/mplot3d/polys3d.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_mplot3d_polys3d.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_mplot3d_polys3d.py:


====================
Generate 3D polygons
====================

Demonstrate how to create polygons in 3D. Here we stack 3 hexagons.

.. GENERATED FROM PYTHON SOURCE LINES 8-37

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from mpl_toolkits.mplot3d.art3d import Poly3DCollection

    # Coordinates of a hexagon
    angles = np.linspace(0, 2 * np.pi, 6, endpoint=False)
    x = np.cos(angles)
    y = np.sin(angles)
    zs = [-3, -2, -1]

    # Close the hexagon by repeating the first vertex
    x = np.append(x, x[0])
    y = np.append(y, y[0])

    verts = []
    for z in zs:
        verts.append(list(zip(x*z, y*z, np.full_like(x, z))))
    verts = np.array(verts)

    ax = plt.figure().add_subplot(projection='3d')

    poly = Poly3DCollection(verts, alpha=.7)
    ax.add_collection3d(poly)
    ax.set_aspect('equalxy')

    plt.show()


.. GENERATED FROM PYTHON SOURCE LINES 38-42

.. tags::
   plot-type: 3D,
   styling: colormap,
   level: intermediate


.. _sphx_glr_download_gallery_mplot3d_polys3d.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

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

    .. container:: sphx-glr-download sphx-glr-download-python

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

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: polys3d.zip <polys3d.zip>`


.. only:: html

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

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
