1
$\begingroup$

Suppose I have a data list

datx = Flatten[#, 1] &@{{#, Sin[#]} & /@ 
Range[-5, 0], {{1, 1}, {2, 1}, {3, 1}, {2, 1.2}, {1.5, 0.5}, {3.5,
  0.5}}, {#, Sin[#]} & /@ Range[4, 7]}

which looks like

ListLinePlot[{datx}, PlotStyle -> {Black}]

enter image description here

you see that datx runs backward at around x=2. Note that this short datx is only an example. The real data list I analyze and plot might longer than 1000.

suppose I have the error bar of the datx

err = 0.1*Range@Length@datx

then I want to plot the uncertainty of datx by

ListLinePlot[{datx, datx + ({0, #} & /@ err), 
 datx - ({0, #} & /@ err)}, PlotStyle -> {Black, Gray, Gray}, 
 Filling -> {2 -> {3}, Gray}]

this looks like

blank area caused by datx running back

of course this will not satisfies me. What I want is something like this:

enter image description here

The red area should be filled If it represents the uncertainty of the data! (and strangely that the Gray in PlotStyle and Filling is not the same color..)

What should I do? I'd like work with ListLinePlot which is convient but a general solution with many Graphics objects will also be accepted

$\endgroup$
4
$\begingroup$

The desired plot can be obtained as follows. Starting with the datax and err as defined in the question, compute the points defining the error boundaries.

upper = datx + ({0, #} & /@ err);
lower = datx - ({0, #} & /@ err);

Then, construct quadrilaterals from all pairs of adjacent points in upper and corresponding points in lower, and plot them.

shading = Graphics[Join[{LightGray}, Polygon /@ Transpose@
    {Most@upper, Rest@upper, Rest@lower, Most@lower}], AspectRatio -> 1/GoldenRatio]

(There are many ways to construct the polygons, but this way is particularly straightforward.)

enter image description here

Also, plot the three curves as in the question, but without Filling.

curves = ListLinePlot[{datx, datx + ({0, #} & /@ err), 
    datx - ({0, #} & /@ err)}, PlotStyle -> {Black, Gray, Gray}];

Finally, combine the two.

Show[shading, curves]

enter image description here

The specified spaces now are filled in. If the Gray error curves are not desired, simply omit them when creating curves.

Addendum

For completeness, the overlapping quadrilaterals can be visualized by

Graphics[Join[{White, EdgeForm[Black], Opacity[0]}, Polygon /@ Transpose@
    {Most@upper, Rest@upper, Rest@lower, Most@lower}], AspectRatio -> 1/GoldenRatio]

enter image description here

Its 3D appearance is an unintended illusion, which disappears when the boundary lines are removed.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.