1
$\begingroup$

I have a list of functions that I want to plot on the same figure. I want each line to have an opacity function that is defined by the list weights. In addition I want each colour of the line to be chosen in order from a colour scheme. My attempt:

parameters = Range[1, 5];
nParam = Length[parameters];
functions = # x & /@ parameters;
weights = Cos[# x]^2 & /@ parameters;
colorFunctions = Table[Opacity[weights[[n]], ColorData["Rainbow"][(n - 1)/(nParam - 1)]], {n, 1, nParam}]
Plot[functions, {x, 0, Pi}, ColorFunction -> colorFunctions]

How do I achieve my goal?

$\endgroup$

2 Answers 2

1
$\begingroup$
parameters = Range[5];
functions = # x & /@ parameters;
nParam = Length[parameters];
colors = ColorData[{"Rainbow", {0, nParam - 1}}] /@ (parameters - 1);
weights = Sin[# x] & /@ parameters;

plots= MapThread[Plot[#, {x, 0, 2 Pi}, ColorFunction -> Function[x, Opacity[##2]]] &,
   {functions, weights, colors}];

Show[plots, PlotRange -> All]

enter image description here

Update:

ClearAll[plotF]
plotF[functions_, range_, weights_, cf_, opts : OptionsPattern[]] := 
 Module[{colors = ColorData[{cf, {0, Length[functions] - 1}}] /@ 
     Range[0, Length[functions] - 1], arg = range[[1]]}, 
  Show[MapThread[Plot[#, range, ColorFunctionScaling -> False, ColorFunction -> Function[Evaluate@arg, Opacity[##2]]] &, 
   {functions, weights, colors}], opts, PlotRange -> All]]

Examples:

plotF[{x, 2 x, 3 x, 4 x, 5 x}, {x, 0, 2 Pi}, {Sin[x], Sin[2 x], 
  Sin[3 x], Sin[4 x], Sin[5 x]}, "Rainbow"]

enter image description here

plotF[{Sin[y], Cos[y], y Sin[y]}, {y, 0, 10}, {Sin[y], Sin[2 y], 
  Sin[3 y]}, "DeepSeaColors", Frame -> True]

enter image description here

$\endgroup$
3
  • $\begingroup$ If possible, could you suggest how to turn this into a function that takes functions and weights as parameters, as well as a plot range (with variable) such as {x, 0, 2 Pi} and produces the same result. BUT such that we can pass different variables. i.e. functions = {Cos[y], Sin[y]} and {y, 0, 10} etc. $\endgroup$
    – Tom
    Commented Mar 4, 2019 at 16:20
  • 1
    $\begingroup$ @Tom, please see the update. $\endgroup$
    – kglr
    Commented Mar 4, 2019 at 17:17
  • $\begingroup$ Thanks for the solution! I have slightly modified your answer, and added ColorFunctionScaling->False so that it works properly. Otherwise it rescales the x-range of the colour function to be in (0,1) $\endgroup$
    – Tom
    Commented Mar 5, 2019 at 14:15
1
$\begingroup$

I figured out a solution. Not very elegant though.

parameters = Range[1, 5];
nParam = Length[parameters];
functions = # x & /@ parameters;
weights = Cos[# x]^2 & /@ parameters;
colorFunction[m_] := With[{n = m}, Function[x , Opacity[Sin[x n], ColorData["Rainbow"][(n - 1)/(nParam - 1)]]]];
plot[n_] := Plot[functions[[n]], {x, 0, Pi}, ColorFunction -> colorFunction[n]]
plots = Table[plot[n], {n, 1, nParam}];
Show[plots, PlotRange -> All]

$\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.