
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/lines_bars_and_markers/fill.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_lines_bars_and_markers_fill.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_lines_bars_and_markers_fill.py:


==============
Filled polygon
==============

`~.Axes.fill()` draws a filled polygon based on lists of point
coordinates *x*, *y*.

This example uses the `Koch snowflake`_ as an example polygon.

.. _Koch snowflake: https://en.wikipedia.org/wiki/Koch_snowflake

.. GENERATED FROM PYTHON SOURCE LINES 14-54

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np


    def koch_snowflake(order, scale=10):
        """
        Return two lists x, y of point coordinates of the Koch snowflake.

        Parameters
        ----------
        order : int
            The recursion depth.
        scale : float
            The extent of the snowflake (edge length of the base triangle).
        """
        def _koch_snowflake_complex(order):
            if order == 0:
                # initial triangle
                angles = np.array([0, 120, 240]) + 90
                return scale / np.sqrt(3) * np.exp(np.deg2rad(angles) * 1j)
            else:
                ZR = 0.5 - 0.5j * np.sqrt(3) / 3

                p1 = _koch_snowflake_complex(order - 1)  # start points
                p2 = np.roll(p1, shift=-1)  # end points
                dp = p2 - p1  # connection vectors

                new_points = np.empty(len(p1) * 4, dtype=np.complex128)
                new_points[::4] = p1
                new_points[1::4] = p1 + dp / 3
                new_points[2::4] = p1 + dp * ZR
                new_points[3::4] = p1 + dp / 3 * 2
                return new_points

        points = _koch_snowflake_complex(order)
        x, y = points.real, points.imag
        return x, y









.. GENERATED FROM PYTHON SOURCE LINES 55-56

Basic usage:

.. GENERATED FROM PYTHON SOURCE LINES 56-64

.. code-block:: Python


    x, y = koch_snowflake(order=5)

    plt.figure(figsize=(8, 8))
    plt.axis('equal')
    plt.fill(x, y)
    plt.show()




.. image-sg:: /gallery/lines_bars_and_markers/images/sphx_glr_fill_001.png
   :alt: fill
   :srcset: /gallery/lines_bars_and_markers/images/sphx_glr_fill_001.png, /gallery/lines_bars_and_markers/images/sphx_glr_fill_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 65-68

Use keyword arguments *facecolor* and *edgecolor* to modify the colors
of the polygon. Since the *linewidth* of the edge is 0 in the default
Matplotlib style, we have to set it as well for the edge to become visible.

.. GENERATED FROM PYTHON SOURCE LINES 68-79

.. code-block:: Python


    x, y = koch_snowflake(order=2)

    fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(9, 3),
                                        subplot_kw={'aspect': 'equal'})
    ax1.fill(x, y)
    ax2.fill(x, y, facecolor='lightsalmon', edgecolor='orangered', linewidth=3)
    ax3.fill(x, y, facecolor='none', edgecolor='purple', linewidth=3)

    plt.show()




.. image-sg:: /gallery/lines_bars_and_markers/images/sphx_glr_fill_002.png
   :alt: fill
   :srcset: /gallery/lines_bars_and_markers/images/sphx_glr_fill_002.png, /gallery/lines_bars_and_markers/images/sphx_glr_fill_002_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 80-93

.. admonition:: References

   The use of the following functions, methods, classes and modules is shown
   in this example:

   - `matplotlib.axes.Axes.fill` / `matplotlib.pyplot.fill`
   - `matplotlib.axes.Axes.axis` / `matplotlib.pyplot.axis`

.. tags::

   styling: shape
   level: beginner
   purpose: showcase


.. _sphx_glr_download_gallery_lines_bars_and_markers_fill.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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