2
$\begingroup$

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

Thanks.

$\endgroup$
  • 2
    $\begingroup$ 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,. $\endgroup$ – DavidC Nov 29 '14 at 17:08
  • 1
    $\begingroup$ 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. $\endgroup$ – rhermans Nov 29 '14 at 17:11
2
$\begingroup$

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[[2]]]]
(* {{1, -2}, {6, -4}, {7, -6.5}} *)
listPos = Cases[ptList, x_ /; Positive[x[[2]]]]
(* {{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:

Color graph

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[[2]]]], Cases[ptList, x_ /; Negative[x[[2]]]], Cases[ptList, x_ /; x[[2]] == 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.

$\endgroup$
3
$\begingroup$

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]

enter image description here

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[30];
styleddata2 = Style[#, cf@#[[2]], PointSize[Large]] & /@ data2;
ListPlot[styleddata2, ImageSize -> 300]

enter image description here

ListPlot[{data2, styleddata2}, Joined -> {True, False}, ImageSize -> 300,
 Filling -> {1 -> {Axis, {LightRed, LightBlue}}}]

enter image description here

$\endgroup$
1
$\begingroup$

Not brilliant, but working:

data = Table[{i, RandomInteger[{-2, 2}]}, {i, 200}];
crit = {#[[2]] < 0 &, #[[2]] == 0 &, #[[2]] > 0 &};
ListPlot[Through[(Select /@ crit)[data]], PlotStyle -> {Red, Green, Blue}]

enter image description here

Other ideas utilizing ColorFunction can be found 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.