
.. 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

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

        Click :ref:`here <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 polygons to fill under 3D line graph
=============================================

Demonstrate how to create polygons which fill the space under a line
graph. In this example polygons are semi-transparent, creating a sort
of 'jagged stained glass' effect.

.. GENERATED FROM PYTHON SOURCE LINES 10-44



.. image-sg:: /gallery/mplot3d/images/sphx_glr_polys3d_001.png
   :alt: polys3d
   :srcset: /gallery/mplot3d/images/sphx_glr_polys3d_001.png, /gallery/mplot3d/images/sphx_glr_polys3d_001_2_0x.png 2.0x
   :class: sphx-glr-single-img





.. code-block:: default


    from matplotlib.collections import PolyCollection
    import matplotlib.pyplot as plt
    import numpy as np
    from scipy.stats import poisson

    # Fixing random state for reproducibility
    np.random.seed(19680801)


    def polygon_under_graph(x, y):
        """
        Construct the vertex list which defines the polygon filling the space under
        the (x, y) line graph. This assumes x is in ascending order.
        """
        return [(x[0], 0.), *zip(x, y), (x[-1], 0.)]


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

    x = np.linspace(0., 10., 31)
    lambdas = range(1, 9)

    # verts[i] is a list of (x, y) pairs defining polygon i.
    verts = [polygon_under_graph(x, poisson.pmf(l, x)) for l in lambdas]
    facecolors = plt.colormaps['viridis_r'](np.linspace(0, 1, len(verts)))

    poly = PolyCollection(verts, facecolors=facecolors, alpha=.7)
    ax.add_collection3d(poly, zs=lambdas, zdir='y')

    ax.set(xlim=(0, 10), ylim=(1, 9), zlim=(0, 0.35),
           xlabel='x', ylabel=r'$\lambda$', zlabel='probability')

    plt.show()


.. _sphx_glr_download_gallery_mplot3d_polys3d.py:


.. only :: html

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



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

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



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

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


.. only:: html

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

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
