2
$\begingroup$

I would like to plot a two dimensional list (ListPLot [{x, y[x]}]) where the background color is changing with x value according to the value assumend by an external function f[x]. Thank you. Sorry, I have to be more precise. This is what I mean fo the function Plot

f[x_] := 6 E^-x Sin[2*Pi*x^2];
g[x_] := Sin[2*x*10];
Plot[{f[x], ConditionalExpression[0, g[x] < 0]}, {x, 0, 2}, 
PlotStyle -> {Black, None}, 
Filling -> {2 -> {Bottom, Red}, 2 -> {Top, Red}}]

I would like to be the same working with list and ListLinePLot.

$\endgroup$
3
  • 2
    $\begingroup$ Can you make the question more specific and concrete by assigning functions to y and f? $\endgroup$
    – Syed
    Mar 19, 2023 at 11:51
  • $\begingroup$ Are you really talking about background color (e.g. something like ContourPlot), or do you want to change the color of the individual points according to your function f[x]? $\endgroup$
    – Lukas Lang
    Mar 19, 2023 at 13:30
  • $\begingroup$ I think the OP wants a DensityPlot in the background with a ListLinePlot in the foreground. $\endgroup$
    – Syed
    Mar 19, 2023 at 13:34

1 Answer 1

2
$\begingroup$
  • Plot.
f[x_] := 6 E^-x Sin[2*Pi*x^2];
g[x_] := Sin[2*x*10];
Plot[{f[x], f[x]}, {x, 0, 2}, 
 ColorFunction -> Function[{x, y}, ColorData["TemperatureMap"][g@x]], 
 ColorFunctionScaling -> True, Filling -> {{1 -> Bottom}, {2 -> Top}},
  Epilog -> 
  Plot[f[x], {x, 0, 2}, 
    PlotStyle -> {AbsoluteThickness[2], White}][[1]]]

enter image description here

  • ListPlot+ Joined -> True
f[x_] := 6 E^-x Sin[2*Pi*x^2];
g[x_] := Sin[2*x*10];
data = Table[{x, f[x]}, {x, 0, 2, .1}];
ListPlot[{data, data}, Joined -> True, 
 ColorFunction -> Function[{x, y}, ColorData["TemperatureMap"][g@x]], 
 ColorFunctionScaling -> True, Filling -> {{1 -> Bottom}, {2 -> Top}},
  Epilog -> ListPlot[data, PlotStyle -> White, Joined -> True][[1]]]

enter image description here

  • ListPlot+ Joined -> False

enter image description here

$\endgroup$
2
  • $\begingroup$ No, I would like to work with the list, so ListPlot, not PLot. $\endgroup$ Mar 19, 2023 at 14:24
  • $\begingroup$ This solution is quite good but seems rather contrived; thank you. Is there another solution? More direct. Besides, a red stripe is sufficient! Directly on the background $\endgroup$ Mar 20, 2023 at 6:56

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.