# ListPlot with colors [duplicate]

Let's create some sample data

n = 100;
d00 = Table[{RandomReal[{-1, 1}], RandomReal[{-4, 4}], RandomReal[{-1, 1}]}, {i, 1, n}];


Now I want the following: create a list plot with the first element of the list as $x$ coordinate and the third element as the $y$ coordinate.

d0 = Table[{d00[[i, 1]], d00[[i, 3]]}, {i, 1, Length[d00]}];


The color of each point is controlled by the corresponding second element. In particular, if the real number is between -2 and 2 the point should be green, while if it is lower/equal than -2 or greater/equal than 2 the point should be red.

Any suggestions?

Here's another option:

l = RandomReal[{-4, 4}, {200, 3}];

ListPlot[
List /@ (l[[All, {1, 3}]])
, PlotStyle -> (If[Abs[#[]] < 2, Green, Red] & /@ l)
]


Where you can use any function that returns a color in your styling. You might want have to rescale your data if it's not in the range {0,1}. If you want to change point sizes you can use Directive

ListPlot[
List /@ (l[[All, {1, 3}]])
, PlotStyle -> (If[Abs[#[]] < 2,
Directive[Green, PointSize[0.04]], Red] & /@ l)
]


This gives: Basically a duplicate of this question.

You can just style each point before you pass them to ListPlot for things like this.

Define a color function:

cfun = Piecewise[{{White, # <= -2}, {Green, -2 < # < 2}, {Red, # > 2}}, Black] &


Then style by second element:

d0 = Table[Style[{d00[[i, 1]], d00[[i, 3]]}, cfun[d00[[i, 2]]]], {i, 1, Length[d00]}];

ListPlot[d0, Background -> Lighter[Gray, 0.5]] Also Specifying PlotMarkers

Using the PlotMarkers option will mask the Style definitions, unless you specify the undocumented:

Method -> {"OptimizePlotMarkers" -> False}


Like so:

GraphicsRow[{
ListPlot[d0, PlotMarkers -> {Automatic, 5}],
ListPlot[d0, PlotMarkers -> {Automatic, 5}, Method -> {"OptimizePlotMarkers" -> False}]
}] • Nice! And if I want to control the size of the points? May 5 '16 at 20:03
• Heh, yeah, using the PlotMarkers option will mask the Style specifications. Instead you can use PlotStyle -> PointSize[0.03] May 5 '16 at 20:13
• If I use ListPlot[d0, PlotStyle -> PointSize[0.02]] then around each red and green dot there is a blue circle! May 5 '16 at 20:15
• How strange, I don't see that. Regardless, a more robust fix appears to be to set Method -> {"OptimizePlotMarkers" -> False} then you can actually use PlotMarkers as normal. May 5 '16 at 20:16
• Now you lost me! Can you make an edit? May 5 '16 at 20:18

Just some variants:

l = RandomReal[{-4, 4}, {200, 3}];
With[{g = #[[All, {1, 3}]] & /@ GatherBy[l, #[] < 2 &]},
ListPlot[g, PlotStyle -> {Red, Green}, PlotMarkers -> {Automatic, 8}]]
ListPlot[Last@Reap[Sow[{#1, #3}, #2 < 2] & @@@ l, _, #2 &],
PlotStyle -> {Red, Green}, PlotMarkers -> {Automatic, 8}]
ListPlot[GroupBy[l, #[] < 2 &, #[[All, {1, 3}]] &],
PlotStyle -> {Red, Green}, PlotMarkers -> {Automatic, 8}]


Outputs: Another way is to use Graphics which gives you more control over your plot.

n = 100;
d00 = Table[{RandomReal[{-1, 1}], RandomReal[{-4, 4}],
RandomReal[{-1, 1}]}, {i, 1, n}];

{x1, x2} = {Min[#], Max[#]} &@d00[[All, 2]];
col[x_] := If[Abs[x] < 2, Green, Red]  (*for color*)
scale[x_] := (x - x1)/(x2 - x1)/20     (*for point size*)
Graphics[{col[#[]], Disk[#[[{1, 3}]], scale[#[]]]} & /@ d00, Frame -> True] ListPlot with styled data

styleddata1 = With[{ps = Rescale[#2, Through[{Min, Max}@d00[[All, 2]]], {5, 20}]},
Style[Tooltip[{#, #3}, #2], If[-2 <= #2 <= 2, Directive[Green, AbsolutePointSize[ps]],
Directive[Red, AbsolutePointSize[ps]]]]] & @@@ d00;

ListPlot[styleddata1, AspectRatio -> 1] BubbleChart with styled data

styleddata2 = Style[Tooltip[{#, #3,
Rescale[#2, Through[{Min, Max}@d00[[All, 2]]], {0.05, 1}]}, #2],
If[-2 <= #2 <= 2, Green, Red]] & @@@ d00;

BubbleChart[styleddata2] 