5
$\begingroup$

I have a list here, which I plot with ListPlot. I wish to color the data points using a special gradient. For example, in the interval $[4, 6]$ (along the y axis), the desired effect is a gradient of Red to LightRed. And for interval $[-1, 3.9]$, a gradient of Green is desired. How can I reach this goal?

enter image description here

To clarify my request, I have tried to show a part of the plot above.

$\endgroup$
7
$\begingroup$

Update: Using Green and Red gradient colors in the two intervals of interest

parts = Select[data, #] & /@ 
  {#[[2]] < -1 || 6 <= #[[2]] &, -1 <= #[[2]] < 4 &, 4 <= #[[2]] < 6 &};
colorfuncs = {Automatic, 
 Function[{x, y}, Blend[{Lighter@Lighter@Green, Darker@Green}, y]], 
 Function[{x, y}, Blend[{Darker@Red, Lighter@Lighter@Red}, y]]};

Show[ListPlot[#, ColorFunction -> #2, Joined -> True, BaseStyle -> PointSize[Medium]]&@@@ 
   Transpose[{parts, colorfuncs2}], GridLines -> {None, {-1, 4, 6}}, 
  Frame -> True, Axes -> False] /. Line -> Point

enter image description here

Replace Lighter@Lighter@Green and Lighter@Lighter@Red with White in ColorFunction to get

enter image description here

Original answer: FWIW

You can use Style on individual data points to color each point based on its second component:

styleddata = Piecewise[{{Style[#, Lighter@Lighter@Green], #[[2]] < -1}, 
 {Style[#,  Darker@Green], -1 <= #[[2]] <= 4}, 
 {Style[#, Darker@Red], 4 <= #[[2]] <= 6}, 
 {Style[#, Lighter@Lighter@Red], 6 <= #[[2]]}}] & /@ data;

ListPlot[styleddata, GridLines -> {None, {-1, 4, 6}}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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