Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3b79ad9

Browse files
committed
Improve curve_error_band example.
- Use finite differences to estimate derivative rather than splines (which are conceptually much more complicated).- Include a "constant error" case, which shows that the band does have constant width in that case.- Force the axes aspect to 1 (so that constant width actually appears constant).- Use np.full() to set up the codes array.
1 parent1daed58 commit3b79ad9

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

‎examples/lines_bars_and_markers/curve_error_band.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
# sphinx_gallery_thumbnail_number = 2
1111

1212
importnumpyasnp
13-
fromscipy.interpolateimportsplprep,splev
1413

1514
importmatplotlib.pyplotasplt
1615
frommatplotlib.pathimportPath
@@ -22,8 +21,8 @@
2221
x,y=r*np.cos(t),r*np.sin(t)
2322

2423
fig,ax=plt.subplots()
25-
ax.plot(x,y)
26-
plt.show()
24+
ax.plot(x,y,"k")
25+
ax.set(aspect=1)
2726

2827
#############################################################################
2928
# An error band can be used to indicate the uncertainty of the curve.
@@ -39,34 +38,41 @@
3938
# `~.Axes.fill_between` method (see also
4039
# :doc:`/gallery/lines_bars_and_markers/fill_between_demo`).
4140

42-
# Error amplitudes depending on the curve parameter *t*
43-
# (actual values are arbitrary and only for illustrative purposes):
44-
err=0.05*np.sin(2*t)**2+0.04+0.02*np.cos(9*t+2)
45-
46-
# calculate normals via derivatives of splines
47-
tck,u=splprep([x,y],s=0)
48-
dx,dy=splev(u,tck,der=1)
49-
l=np.hypot(dx,dy)
50-
nx=dy/l
51-
ny=-dx/l
41+
defdraw_error_band(ax,x,y,err,**kwargs):
42+
# Calculate normals via centered finite differences (except the first point
43+
# which uses a forward difference and the last point which uses a backward
44+
# difference).
45+
dx=np.concatenate([[x[1]-x[0]],x[2:]-x[:-2], [x[-1]-x[-2]]])
46+
dy=np.concatenate([[y[1]-y[0]],y[2:]-y[:-2], [y[-1]-y[-2]]])
47+
l=np.hypot(dx,dy)
48+
nx=dy/l
49+
ny=-dx/l
5250

53-
# end points of errors
54-
xp=x+nx*err
55-
yp=y+ny*err
56-
xn=x-nx*err
57-
yn=y-ny*err
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
5856

59-
vertices=np.block([[xp,xn[::-1]],
60-
[yp,yn[::-1]]]).T
61-
codes=Path.LINETO*np.ones(len(vertices),dtype=Path.code_type)
62-
codes[0]=codes[len(xp)]=Path.MOVETO
63-
path=Path(vertices,codes)
57+
vertices=np.block([[xp,xn[::-1]],
58+
[yp,yn[::-1]]]).T
59+
codes=np.full(len(vertices),Path.LINETO)
60+
codes[0]=codes[len(xp)]=Path.MOVETO
61+
path=Path(vertices,codes)
62+
ax.add_patch(PathPatch(path,**kwargs))
6463

65-
patch=PathPatch(path,facecolor='C0',edgecolor='none',alpha=0.3)
64+
axs= (plt.figure(constrained_layout=True)
65+
.subplots(1,2,sharex=True,sharey=True))
66+
errs= [
67+
(axs[0],"constant error",0.05),
68+
(axs[1],"variable error",0.05*np.sin(2*t)**2+0.04),
69+
]
70+
fori, (ax,title,err)inenumerate(errs):
71+
ax.set(title=title,aspect=1,xticks=[],yticks=[])
72+
ax.plot(x,y,"k")
73+
draw_error_band(ax,x,y,err=err,
74+
facecolor=f"C{i}",edgecolor="none",alpha=.3)
6675

67-
fig,ax=plt.subplots()
68-
ax.plot(x,y)
69-
ax.add_patch(patch)
7076
plt.show()
7177

7278
#############################################################################

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp