# ListPlot - coloring negative and positive points

how to create a point graph(ListPlot?), where the positive values will be BLUE, negative RED and 0 values will be green?

Thanks.

• The best answer was chosen after 43 minutes, 8 minutes after the first submission was made, after only 19 views and zero votes, while Asians and Pacific Islanders sleep,. Nov 29 '14 at 17:08
• Welcome to Mma.SE, please take the tour and learn the basics of the site. Once you gain enough reputation by making good questions you will be able to vote up both questions and answers. Don't rush, you may want stay vigilant some time after you get the first answer as its likely that the best approaches may come later improving over a previous reply. Wait 24hours for other answers before accepting the best. Nov 29 '14 at 17:11

The first solution is a basic one, but it does the trick. For a second solution involving ColorFunction, see below.

I assume that you have a list of points, such as

ptList = {{1, -2}, {2, 5}, {3, 6}, {4, 0}, {5, 1}, {6, -4}, {7, -6.5}, {8,  2}, {9, 0}, {10, 5}};


and that you are interested in whether the second coordinate is postive, negative or zero. First you can extract the negative and positive elements from the list:

listNeg = Cases[ptList, x_ /; Negative[x[]]]
(* {{1, -2}, {6, -4}, {7, -6.5}} *)
listPos = Cases[ptList, x_ /; Positive[x[]]]
(* {{2, 5}, {3, 6}, {5, 1}, {8, 2}, {10, 5}} *)


The rest is automatically zero:

listZero = Complement[ptList, Join[listNeg, listPos]]
(* {{4, 0}, {9, 0}} *)


Then you can plot the three different lists, each with their own color:

ListPlot[{listPos, listNeg, listZero}, PlotStyle -> {Blue, Red, Green}]


which gives the result: Alternatively, you can put everything into one ListPlot command, which may save some memory when you are dealing with large lists. In that case, you can no longer use Complement to find the zero elements, but you should check for them instead:

ListPlot[{Cases[ptList, x_ /; Positive[x[]]], Cases[ptList, x_ /; Negative[x[]]], Cases[ptList, x_ /; x[] == 0]}, PlotStyle -> {Blue, Red, Green}]


This gives you the same result as before.

Edit: a second solution using ColorFunction

Based on the question that funnypony referred to and based on an answer to this question, here is a second solution.

First you define your own ColorFunction using Piecewise

myColorFunc[x_] := Piecewise[{{Blue, x > 0}, {Green, x == 0}, {Red, x < 0}}]


Then, you use ListLinePlot with a replacement rule

ListLinePlot[ptList, ColorFunction -> myColorFunc, ColorFunctionScaling -> False] /. Line[arg___] :> Point[arg]


which gives the same result as before, albeit with square plot markers instead of round ones.

You can Style individual data points without having to partition the dataset:

data = RandomInteger[{-3, 3}, 30];
cf = Function[{y}, Piecewise[{{Red, y < 0}, {Blue, y > 0}}, Green]];
styleddata = Style[#, cf@#, PointSize[Large]] & /@ data;
ListPlot[styleddata, ImageSize -> 300] If the data is a list of pairs, apply the coloring function cf to the second elements of the pairs:

data2 = {#, RandomInteger[{-3, 3}]} & /@ Range;
styleddata2 = Style[#, cf@#[], PointSize[Large]] & /@ data2;
ListPlot[styleddata2, ImageSize -> 300] ListPlot[{data2, styleddata2}, Joined -> {True, False}, ImageSize -> 300,
Filling -> {1 -> {Axis, {LightRed, LightBlue}}}] Not brilliant, but working:

data = Table[{i, RandomInteger[{-2, 2}]}, {i, 200}];
crit = {#[] < 0 &, #[] == 0 &, #[] > 0 &};
ListPlot[Through[(Select /@ crit)[data]], PlotStyle -> {Red, Green, Blue}] Other ideas utilizing ColorFunction can be found here