# Adding a gradient filling according to a given function between two curves

Consider the following sample code

f1[x_]:=x;
f2[x_]:=2x;
c[y_]:= Exp[-y];

Plot[{f1[x], f2[x]}, {x, 0, 1},
PlotStyle -> {ColorData["SolarColors", 0], ColorData["SolarColors", 1]}]

Plot[c[2 x - x], {x, 0, 1}]


I would like to have a filling that fills the area between the two curves f1[x],f2[x] by the values of c[f2[x]-f1[x]] in the following manner

1. That the values of c[f1[x]] and c[f2[x]] will be the end values of the colordata.
2. The area in between them will be a gradient fill with the colordata that is normalized according to c[f2[x]-f1[x]]

What I want is best presented in the figure • 1. It seems that the values of $c(x)$ do not fit between the two curves though. 2. What property of the filling should be influenced by the value of $c(x)$? Should the filling be within the two curves, or from one of the curves to the axis? I think you could perhaps clarify your question a bit. – MarcoB May 9 '18 at 13:20
• @MarcoB. Thanks, I've edited the question and title, hope it is better understood now. – jarhead May 9 '18 at 13:50

Is this what you are looking for?

Plot[{f1[x], f2[x]}, {x, 0, 1}, Filling -> {1 -> {2}},
ColorFunction -> Function[{x, y},
Blend[{ColorData["SolarColors", 0], ColorData["SolarColors", 1]},
c[(y - f2[x])/(f1[x] - f2[x])]]],
ColorFunctionScaling -> False
] It looks better if you normalise $\exp$ to the range $[0,1]$ instead of $[0,e^{-1}]\approx [0,.4]$:

c[y_] := (Exp[-y] - Exp[-1])/(1 - Exp[-1]) • you should use ColorFunctionScaling->False here (this only works because your scale is 0-1 anyway.) – george2079 May 10 '18 at 18:06
• @george2079 Thank you, but surely I must be missing something: if I add your option to my code, the figure becomes solid yellow: i.imgur.com/jtM7COp.png Do you know why? – AccidentalFourierTransform May 10 '18 at 19:15
• you have the x and y reversed in the color function, c[(y - f1[x])/(f2[x] - f1[x])] – george2079 May 10 '18 at 19:54
• @george2079 perfect, thank you! – AccidentalFourierTransform May 10 '18 at 19:59

You can also use parametricPlot with (ColorData["SolarColors"][c@#4] &) as the ColorFunction setting:

ParametricPlot[{x, t f1[x] + (1 - t) f2[x]}, {x, 0, 1}, {t, 0, 1},
AspectRatio -> 1, Mesh -> None, BoundaryStyle -> None,
ColorFunction -> (ColorData["SolarColors"][c@#4] &)] 