{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Filled polygon\n\n`~.Axes.fill()` draws a filled polygon based on lists of point\ncoordinates *x*, *y*.\n\nThis example uses the `Koch snowflake`_ as an example polygon.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\n\ndef koch_snowflake(order, scale=10):\n    \"\"\"\n    Return two lists x, y of point coordinates of the Koch snowflake.\n\n    Parameters\n    ----------\n    order : int\n        The recursion depth.\n    scale : float\n        The extent of the snowflake (edge length of the base triangle).\n    \"\"\"\n    def _koch_snowflake_complex(order):\n        if order == 0:\n            # initial triangle\n            angles = np.array([0, 120, 240]) + 90\n            return scale / np.sqrt(3) * np.exp(np.deg2rad(angles) * 1j)\n        else:\n            ZR = 0.5 - 0.5j * np.sqrt(3) / 3\n\n            p1 = _koch_snowflake_complex(order - 1)  # start points\n            p2 = np.roll(p1, shift=-1)  # end points\n            dp = p2 - p1  # connection vectors\n\n            new_points = np.empty(len(p1) * 4, dtype=np.complex128)\n            new_points[::4] = p1\n            new_points[1::4] = p1 + dp / 3\n            new_points[2::4] = p1 + dp * ZR\n            new_points[3::4] = p1 + dp / 3 * 2\n            return new_points\n\n    points = _koch_snowflake_complex(order)\n    x, y = points.real, points.imag\n    return x, y"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Basic usage:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x, y = koch_snowflake(order=5)\n\nplt.figure(figsize=(8, 8))\nplt.axis('equal')\nplt.fill(x, y)\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Use keyword arguments *facecolor* and *edgecolor* to modify the colors\nof the polygon. Since the *linewidth* of the edge is 0 in the default\nMatplotlib style, we have to set it as well for the edge to become visible.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x, y = koch_snowflake(order=2)\n\nfig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(9, 3),\n                                    subplot_kw={'aspect': 'equal'})\nax1.fill(x, y)\nax2.fill(x, y, facecolor='lightsalmon', edgecolor='orangered', linewidth=3)\nax3.fill(x, y, facecolor='none', edgecolor='purple', linewidth=3)\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. admonition:: References\n\n   The use of the following functions, methods, classes and modules is shown\n   in this example:\n\n   - `matplotlib.axes.Axes.fill` / `matplotlib.pyplot.fill`\n   - `matplotlib.axes.Axes.axis` / `matplotlib.pyplot.axis`\n\n.. tags::\n\n   styling: shape\n   level: beginner\n   purpose: showcase\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}