最直接简单的思路是,多条曲线,只填充其中需要填充的:
With[{dist := PDF[NormalDistribution[]]},
Plot[{dist[x], If[x < 1 && x > -2, dist[x]]}, {x, -5, 5},
Filling -> {2 -> {Axis, Directive[Opacity[0.45], Blue]}},
AspectRatio -> .85/GoldenRatio, PlotStyle -> Blue,
AxesOrigin -> {0, 0}]]
但是这样搞分段函数,调整区间的情况下不是很方便; 利用参数方程绘图的区块绘制功能则方便
With[{pdf = PDF[NormalDistribution[]], cv = 1},
ParametricPlot[{x, t pdf[x]}, {x, -5, 5}, {t, 0, 1},
MeshFunctions -> {#3 &}, Mesh -> {{-cv}},
MeshShading -> {Directive[Opacity[0.42], ColorData[2][6]], None},
AspectRatio -> 1/GoldenRatio, PlotRange -> All, Frame -> False]]
看个复杂点的效果,也是一口气完成:
With[{pdf = PDF[NormalDistribution[]], cv = 2},
ParametricPlot[{x, t pdf[x]}, {x, -5, 5}, {t, 0, 1},
MeshFunctions -> {#3 &}, Mesh -> {{-cv, 0, 1, 2, 3}},
MeshShading -> {None, Directive[Opacity[0.42], ColorData[1][2]]},
AspectRatio -> 1/GoldenRatio, PlotRange -> All, Frame -> False]]
对比下面的区别:
With[{pdf = PDF[NormalDistribution[]], cv = 2},
ParametricPlot[{x, t pdf[x]}, {x, -5, 5}, {t, 0, 1},
MeshFunctions -> {#3 &}, Mesh -> {{-cv, cv}},
MeshShading -> {None, Directive[Opacity[0.42], ColorData[1][2]]},
AspectRatio -> 1/GoldenRatio, PlotRange -> All, Frame -> False]]
With[{pdf = PDF[NormalDistribution[]], cv = 2},
ParametricPlot[{x, t pdf[x]}, {x, -5, 5}, {t, 0, 1},
MeshFunctions -> {#3 &}, Mesh -> {{-cv, cv}},
MeshShading -> {Directive[Opacity[0.42], ColorData[1][2]], None},
AspectRatio -> 1/GoldenRatio, PlotRange -> All, Frame -> False]]
ParametricPlot[
r^2 { Sqrt[t] Cos[t], 3 Sin[t]}, {t, 0, 6 Pi}, {r, 1.8, 2},
MeshFunctions -> {#3 &}, Mesh -> {Range[1, 100]/5},
MeshShading -> {None, Directive[Opacity[0.42], ColorData[1][2]]},
Axes -> False, Frame -> False]
ParametricPlot[{u {-Sin[t], Cos[t]}, u {Sin[t], Cos[t]}}, {t, 0,
Pi}, {u, 0, 1},
MeshFunctions -> {(Boole[(
If[#1 <= 0,
#2 - Sqrt[0.25 - (#1 + 0.5)^2] >= 0 || (#1 + .5)^2 + #2^2 <
1/64,
#2 + Sqrt[0.25 - (#1 - 0.5)^2] >= 0 && (#1 - .5)^2 + #2^2 >
1/64]
)] &)}, Mesh -> {{0.1}}, MeshShading -> {Blue, Red},
PlotPoints -> 70,
Frame -> False, Axes -> False, BoundaryStyle -> None,
ImageSize -> Large]
参考:
http://mathematica.stackexchange.com/questions/37103/plotting-confidence-intervals
http://mathematica.stackexchange.com/questions/25169/how-to-plot-filling-under-a-curve/25216#25216