# Filling in ListLinePlot with self intersection problem and turn-round track/multi-valued line

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}]


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

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

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

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

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]


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

Graphics[Join[{White, EdgeForm[Black], Opacity[0]}, Polygon /@ Transpose@