|
1 | 1 | """
|
2 | 2 | ==============
|
3 |
| -Fill plot demo |
| 3 | +Filled polygon |
4 | 4 | ==============
|
5 | 5 |
|
6 |
| -Demo fill plot. |
| 6 | +`~.Axes.fill()` draws a filled polygon based based on lists of point |
| 7 | +coordinates *x*, *y*. |
| 8 | +
|
| 9 | +This example uses the `Koch snowflake`_ as an example polygon. |
| 10 | +
|
| 11 | +.. _Koch snowflake: https://en.wikipedia.org/wiki/Koch_snowflake |
| 12 | +
|
7 | 13 | """
|
8 | 14 |
|
9 |
| -############################################################################### |
10 |
| -# First, the most basic fill plot a user can make with matplotlib: |
11 | 15 | import numpy as np
|
12 | 16 | import matplotlib.pyplot as plt
|
13 | 17 |
|
14 |
| -x = [0, 1, 2, 1] |
15 |
| -y = [1, 2, 1, 0] |
16 | 18 |
|
17 |
| -fig, ax = plt.subplots() |
18 |
| -ax.fill(x, y) |
19 |
| -plt.show() |
| 19 | +def koch_snowflake(order, scale=10): |
| 20 | + """ |
| 21 | + Return two lists x, y of point coordinates of the Koch snowflake. |
| 22 | +
|
| 23 | + Arguments |
| 24 | + --------- |
| 25 | + order : int |
| 26 | + The recursion depth. |
| 27 | + scale : float |
| 28 | + The extent of the snowflake (edge length of the base triangle). |
| 29 | + """ |
| 30 | + def _koch_snowflake_complex(order): |
| 31 | + if order == 0: |
| 32 | + # initial triangle |
| 33 | + angles = np.array([0, 120, 240]) + 90 |
| 34 | + return scale / np.sqrt(3) * np.exp(np.deg2rad(angles) * 1j) |
| 35 | + else: |
| 36 | + ZR = 0.5 - 0.5j * np.sqrt(3) / 3 |
| 37 | + |
| 38 | + p1 = _koch_snowflake_complex(order - 1) # start points |
| 39 | + p2 = np.roll(p1, shift=-1) # end points |
| 40 | + dp = p2 - p1 # connection vectors |
| 41 | + |
| 42 | + new_points = np.empty(len(p1) * 4, dtype=np.complex128) |
| 43 | + new_points[::4] = p1 |
| 44 | + new_points[1::4] = p1 + dp / 3 |
| 45 | + new_points[2::4] = p1 + dp * ZR |
| 46 | + new_points[3::4] = p1 + dp / 3 * 2 |
| 47 | + return new_points |
| 48 | + |
| 49 | + points = _koch_snowflake_complex(order) |
| 50 | + x, y = points.real, points.imag |
| 51 | + return x, y |
| 52 | + |
20 | 53 |
|
21 | 54 | ###############################################################################
|
22 |
| -# Next, a few more optional features: |
23 |
| -# |
24 |
| -# * Multiple curves with a single command. |
25 |
| -# * Setting the fill color. |
26 |
| -# * Setting the opacity (alpha value). |
| 55 | +# Basic usage: |
27 | 56 |
|
| 57 | +x, y = koch_snowflake(order=5) |
28 | 58 |
|
29 |
| -x = np.linspace(0, 1.5 * np.pi, 500) |
30 |
| -y1 = np.sin(x) |
31 |
| -y2 = np.sin(3 * x) |
| 59 | +plt.figure(figsize=(8, 8)) |
| 60 | +plt.axis('equal') |
| 61 | +plt.fill(x, y) |
| 62 | +plt.show() |
32 | 63 |
|
33 |
| -fig, ax = plt.subplots() |
| 64 | +############################################################################### |
| 65 | +# Use keyword arguments *facecolor* and *edgecolor* to modify the the colors |
| 66 | +# of the polygon. Since the *linewidth* of the edge is 0 in the default |
| 67 | +# Matplotlib style, we have to set it as well for the edge to become visible. |
34 | 68 |
|
35 |
| -ax.fill(x, y1, 'b', x, y2, 'r', alpha=0.3) |
| 69 | +x, y = koch_snowflake(order=2) |
36 | 70 |
|
37 |
| -# Outline of the region we've filled in |
38 |
| -ax.plot(x, y1, c='b', alpha=0.8) |
39 |
| -ax.plot(x, y2, c='r', alpha=0.8) |
40 |
| -ax.plot([x[0], x[-1]], [y1[0], y1[-1]], c='b', alpha=0.8) |
41 |
| -ax.plot([x[0], x[-1]], [y2[0], y2[-1]], c='r', alpha=0.8) |
| 71 | +fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(9, 3), |
| 72 | + subplot_kw={'aspect': 'equal'}) |
| 73 | +ax1.fill(x, y) |
| 74 | +ax2.fill(x, y, facecolor='lightsalmon', edgecolor='orangered', linewidth=3) |
| 75 | +ax3.fill(x, y, facecolor='none', edgecolor='purple', linewidth=3) |
42 | 76 |
|
43 | 77 | plt.show()
|
| 78 | + |
| 79 | +############################################################################# |
| 80 | +# |
| 81 | +# ------------ |
| 82 | +# |
| 83 | +# References |
| 84 | +# """""""""" |
| 85 | +# |
| 86 | +# The use of the following functions, methods, classes and modules is shown |
| 87 | +# in this example: |
| 88 | + |
| 89 | +import matplotlib |
| 90 | +matplotlib.axes.Axes.fill |
| 91 | +matplotlib.pyplot.fill |
| 92 | +matplotlib.axes.Axes.axis |
| 93 | +matplotlib.pyplot.axis |
0 commit comments