Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1464e9

Browse files
authored
Merge pull request #14066 from meeseeksmachine/auto-backport-of-pr-14053-on-v3.1.x
Backport PR #14053 on branch v3.1.x (Improve fill() example)
2 parents 1798bb6 + d9f0bfe commit c1464e9

File tree

2 files changed

+75
-24
lines changed

2 files changed

+75
-24
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ per-file-ignores =
137137
examples/images_contours_and_fields/triplot_demo.py: E201, E402
138138
examples/images_contours_and_fields/watermark_image.py: E402
139139
examples/lines_bars_and_markers/errorbar_limits_simple.py: E402
140+
examples/lines_bars_and_markers/fill.py: E402
140141
examples/lines_bars_and_markers/fill_between_demo.py: E402
141142
examples/lines_bars_and_markers/filled_step.py: E402
142143
examples/lines_bars_and_markers/joinstyle.py: E402
Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,93 @@
11
"""
22
==============
3-
Fill plot demo
3+
Filled polygon
44
==============
55
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+
713
"""
814

9-
###############################################################################
10-
# First, the most basic fill plot a user can make with matplotlib:
1115
import numpy as np
1216
import matplotlib.pyplot as plt
1317

14-
x = [0, 1, 2, 1]
15-
y = [1, 2, 1, 0]
1618

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+
2053

2154
###############################################################################
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:
2756

57+
x, y = koch_snowflake(order=5)
2858

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()
3263

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

35-
ax.fill(x, y1, 'b', x, y2, 'r', alpha=0.3)
69+
x, y = koch_snowflake(order=2)
3670

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)
4276

4377
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

Comments
 (0)