11
$\begingroup$

I'm trying to change the opacity of a line in a ListLinePlot for increasing x-value. E.g. the line should have opacity 1 at beginning and shade than down to opacity along the line. I want to take the opacity value from another list whith the same amount of values as the list used for the ListLinePlot. My current attempt is something like:

ListLinePlot[Table[results[[ind,1]], {ind, 1, 3599}], 
ColorFunction -> (Table[Opacity[results[[ind,2]], Red], {ind, 1, 3599}] &), 
ColorFunctionScaling -> False]

My results data has dimensions {3600,2}. All values of results[[ind,2]] are inbetween 0 and 1.

I would be greatful for every hint. Thank you :)

$\endgroup$
1
  • $\begingroup$ Hey, Thank you already for your very useful comments! $\endgroup$
    – piolio
    Commented Jul 6, 2020 at 14:45

4 Answers 4

9
$\begingroup$

Using the sample data generated by flinty, but affecting the the change in opacity using the ColorFunction option of ListLinePlot:

results = Table[{i, Sin[4 i]/2 + 1/2 + RandomReal[]*0.2}, {i, 0, 2 \[Pi], 0.01}];
ListLinePlot[
 results,
 ColorFunction -> Function[{x, y}, Blend[{Red, White}, x/(2 Pi)]],
 ColorFunctionScaling -> False,
 AspectRatio -> 1/5
 ]

Output

You can also use Transparent instead of White, but I didn't like the result of that as much.

Here's an example with a list of opacities rather than a continuous function:

xvalues = First /@ results;
opacities = #/(2 Pi) & /@ xvalues;
colors = MapThread[{#, RGBColor[1, 0, 0, #]} &, {xvalues, opacities}];
ListLinePlot[
 results,
 ColorFunction -> Function[{x, y}, Blend[colors, 1 - x/(2 Pi)]],
 ColorFunctionScaling -> False,
 AspectRatio -> 1/5
 ]

Output

$\endgroup$
7
$\begingroup$

It's easier to do this sort of thing with Graphics:

results = Table[{i, Sin[4 i]/2 + 1/2 + RandomReal[]*0.2}, {i, 0, 2 π, 0.01}];
colours = RGBColor[1, 0, 0, #[[1]]/(2 π)] & /@ results;
Graphics[{Riffle[colours, Line /@ Partition[results, 2, 1]]}, Axes -> True]

fade out list plot graphics opacity

$\endgroup$
1
  • $\begingroup$ Note I've used the first element #[[1]] which is the x-coordinate. ListLinePlot uses x-coordinates 1,2,3,4... implicitly so you may have to add these if you use Graphics. If you have trouble adapting this to your case, please provide a small amount of sample data and I can make the necessary changes. $\endgroup$
    – flinty
    Commented Jul 5, 2020 at 20:43
6
$\begingroup$

Using results and colours from flinty`s answer:

results = Table[{i, Sin[4 i]/2 + 1/2 + RandomReal[]*0.2}, {i, 0, 2 π, 0.01}];
colours = RGBColor[1, 0, 0, 1 - #[[1]]/(2 π)] & /@ results;

We can use colours as the option value for VertexColors:

Graphics[Line[results, VertexColors -> colours], Axes -> True]

enter image description here

Alternatively,

colours2 = Opacity[1 - #, Red] & /@ Rescale[results[[All, 1]]];
Graphics[Line[results, VertexColors -> colours2], Axes -> True]

enter image description here

$\endgroup$
1
$\begingroup$

it works now! Thanks a lot! I have the impression that my example of a fading line confused more than it helped: I wanted to weight my line with the opacity values from another list. Anyway you ideas where extremely helpful, and I just had to adjust the used function a litte. For people having the same question in the future :

yvalues = 
Table[results[[time, 1]], {time, 1,tend}]; (*y values as list*)

opacities = 
Table[results[[time, 2]], {time, 1, tend}]; (*corresponding opacities as list*)
colors = MapThread[{#1, RGBColor[1, 0, 0, #2]} &, {yvalues, opacities}];
ListLinePlot[yvalues, ColorFunction -> Function[{x, y}, colors[[x, 2]]], 
ColorFunctionScaling -> False, AspectRatio -> 1/5]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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