# Applying a ColorFunction across the width of a line

I would like to apply a ColorFunction across the width of the line so that the line remains bright white at the center and fades to background color shortly thereafter at the edges creating the look of an oscilloscope trace (or at least, that's what I think it will result in). (something similar to this)

Plot[x, {x, -1, 1}
, PlotStyle -> {Thickness[0.04], White}
, Background -> Darker@Cyan
]


From what I understand, the ColorFunction works along the length of the line as follows;

Plot[x, {x, -1, 1}
, PlotStyle -> {Thickness[0.04], White}
, Background -> Darker@Cyan
, ColorFunction -> Hue
]


• Something like ColorFunction -> (Opacity[#1, White] &) and removing the White from the PlotStyle? Commented Jan 7, 2022 at 21:12
• Plot[Sin[x], {x, -4, 4}, PlotStyle -> {Thickness[0.04]}, ColorFunction -> (Opacity[#1, White] &), Background -> Darker@Cyan] results in a gradient in the x-direction shown here.
– Syed
Commented Jan 7, 2022 at 21:17
• Like an oscilloscope trace, bright along the center of the trace and fading width wise as it goes across the screen. (something similar to this).
– Syed
Commented Jan 7, 2022 at 21:24
• For glowing lines you could have a look at this: mathematica.stackexchange.com/a/228763/72682 Commented Jan 7, 2022 at 21:31
• something like ParametricPlot[{x, 1 - t + x}, {x, -1, 1}, {t, -.1, .1}, BoundaryStyle -> None, Background -> Darker@Cyan, ColorFunction -> (Blend[{Darker@Cyan, White, Darker@Cyan}, (#4 + .1)/.2] &), ColorFunctionScaling -> False, AspectRatio -> 1/2]?
– kglr
Commented Jan 7, 2022 at 21:39

It's possible to make textured lines like in this answer and you could use the linear gradient texture from my other answer on glowing graph edges. However, if you need curves, a lot of textured lines (actually polygons) will have gaps and it looks bad.

For something like a scope trace for curves, as mentioned in the comments, it might be better to go with a DensityPlot like this:

plot = Plot[Sin[8.3 x] + 0.5 Cos[4. x], {x, 0, 3}];
line = Cases[plot, Line[_], Infinity] // First;
reg = SignedRegionDistance[line];
DensityPlot[Quiet@Exp[-reg[{x, y}]^2/0.002], {x, 0, 3}, {y, -3, 3},
PlotRange -> All, PlotPoints -> 50, ColorFunction -> "AvocadoColors"]


A better ColorFunction and some axes can make it look more scope-y

cols = {{0., Darker[Green, .8]}, {0.7, Darker[Green, .4]}, {0.85, Green}, {1, White}};
DensityPlot[Quiet@Exp[-reg[{x, y}]^2/0.004], {x, 0, 3}, {y, -3, 3},
PlotRange -> All, PlotPoints -> 50,
ColorFunction -> (Blend[cols, #1] &), GridLines -> Automatic]


Using a slight modification of this answer:

ClearAll[pCurve]
pCurve[f_, width_: 1/2][x_, u_] := {x, f@x} + (1-2 u) width/2 Cross@Normalize[{1, f'@x}]

colorFunc[color_: Red] := Blend[{color, White, color}, #4] &;


Examples:

f1[x_] := x

f2 = # Sin@# &;

color = Darker@Cyan;

ParametricPlot[pCurve[f1][x, t], {x, -3, 3}, {t, 0, 1},
BoundaryStyle -> color, Background -> color,
ColorFunction -> colorFunc[color], Frame -> False, Axes -> True]


ParametricPlot[pCurve[f2, 1][x, t], {x, -2 Pi, 2 Pi}, {t, 0, 1},
BoundaryStyle -> color, Background -> color,
ColorFunction -> colorFunc[color], Frame -> False, Axes -> True]


Plot[x,
{x, -1, 1},
PlotStyle -> Thickness[0.04],
Background -> Darker@Cyan,
ColorFunction -> (Opacity[4 (#1 - #1^2), White] &)
]


Using Haloing[]: (introduced with v13.3)

Framed[#
,Background->GrayLevel[0.5]
,FrameMargins->5
]&@Framed[#
,Background->GrayLevel[0.75]
,FrameMargins->5
]&@
Plot[{Sin[8.3 x] + 0.5 Cos[4. x]}
, {x, 0, 3}
,Axes->False
, PlotStyle -> {White,Haloing[Lighter@Cyan,1,4]}
,Background->Blend[{Cyan,Gray}]
,Frame->True
,GridLines->Automatic
,GridLinesStyle->{{Black,Dotted},{Black,Dotted}}
,Method->{"GridLinesInFront"->True}
,ImageMargins->{{10,20},{10,20}}
]