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

Skip to content

Commit 9be1ed6

Browse files
committed
Add example for drawing an error band around a curve
1 parent 786d18a commit 9be1ed6

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ per-file-ignores =
158158
examples/images_contours_and_fields/tripcolor_demo.py: E201, E402
159159
examples/images_contours_and_fields/triplot_demo.py: E201, E402
160160
examples/images_contours_and_fields/watermark_image.py: E402
161+
examples/lines_bars_and_markers/curve_error_band.py: E402
161162
examples/lines_bars_and_markers/errorbar_limits_simple.py: E402
162163
examples/lines_bars_and_markers/fill.py: E402
163164
examples/lines_bars_and_markers/fill_between_demo.py: E402
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""
2+
=====================
3+
Curve with error band
4+
=====================
5+
6+
This example illustrates how to draw an error band around a parametrized curve.
7+
8+
A parametrized curve x(t), y(t) can directly be drawn using `~.Axes.plot`.
9+
"""
10+
# sphinx_gallery_thumbnail_number = 2
11+
12+
import numpy as np
13+
from scipy.interpolate import splprep, splev
14+
15+
import matplotlib.pyplot as plt
16+
from matplotlib.path import Path
17+
from matplotlib.patches import PathPatch
18+
19+
N = 400
20+
phi = np.linspace(0, 2 * np.pi, N)
21+
r = 0.5 + np.cos(phi)
22+
x, y = r * np.cos(phi), r * np.sin(phi)
23+
24+
fig, ax = plt.subplots()
25+
ax.plot(x, y)
26+
plt.show()
27+
28+
#############################################################################
29+
# An error band can be used to indicate the uncertainty of the the curve.
30+
# In this example we assume that the error can be given as a scalar *err*
31+
# that describes the uncertainty perpendicular to the curve in every point.
32+
#
33+
# We visualize this error as a colored band around the path using a
34+
# `.PathPatch`. The patch is created from two path segments *(xp, yp)*, and
35+
# *(xn, yn)* that are shifted by +/- *err* perpendicular to the curve *(x, y)*.
36+
#
37+
# Note: This method of using a `.PathPatch` is suited for arbitrary curves in
38+
# 2D. If you just have a standard y-vs.-x plot, you can use the simpler
39+
# `~.Axes.fill_between` method (see also
40+
# :doc:`/gallery/lines_bars_and_markers/fill_between_demo`).
41+
42+
err = 0.05 * np.sin(2*phi)**2 + 0.04 + 0.02 * np.cos(9*phi + 2)
43+
44+
# calculate normals via derivatives of splines
45+
tck, u = splprep([x, y], s=0)
46+
dx, dy = splev(u, tck, der=1)
47+
l = np.hypot(dx, dy)
48+
nx = dy / l
49+
ny = -dx / l
50+
51+
# end points of errors
52+
xp = x + nx * err
53+
yp = y + ny * err
54+
xn = x - nx * err
55+
yn = y - ny * err
56+
57+
vertices = np.block([[xp, xn[::-1]],
58+
[yp, yn[::-1]]]).T
59+
codes = Path.LINETO * np.ones(len(vertices), dtype=Path.code_type)
60+
codes[0] = codes[len(xp)] = Path.MOVETO
61+
path = Path(vertices, codes)
62+
63+
patch = PathPatch(path, facecolor='C0', edgecolor='none', alpha=0.3)
64+
65+
fig, ax = plt.subplots()
66+
ax.plot(x, y)
67+
ax.add_patch(patch)
68+
plt.show()
69+
70+
#############################################################################
71+
#
72+
# ------------
73+
#
74+
# References
75+
# """"""""""
76+
#
77+
# The use of the following functions, methods and classes is shown
78+
# in this example:
79+
80+
import matplotlib
81+
matplotlib.patches.PathPatch
82+
matplotlib.path.Path

0 commit comments

Comments
 (0)