1
$\begingroup$

I have to generate 10-50 random real numbers,.. List[ RandomReal[{-10, 10}, {RandomInteger [{10, 10}]}]] This I've done, but I also have to make a ListPlot in which numbers from 10 to 1 will be blue, from 0,99 to -0,99 will be red and from -1 to 10 will be green. I tried to separate the list to 3 lists, I also tried to use conditions but I can't make it work,.. Could someone help?

$\endgroup$
3
  • $\begingroup$ Have you seen Select[]? $\endgroup$ Nov 8, 2017 at 11:45
  • $\begingroup$ There should be a solution that uses ColorFunction but I see nothing in the Help. Anyone? $\endgroup$
    – A.G.
    Nov 8, 2017 at 14:09
  • $\begingroup$ @A.G. - For ListPlot, "ColorFunction requires at least one dataset to be Joined", i.e., it will vary the color of a line not points. $\endgroup$
    – Bob Hanlon
    Nov 8, 2017 at 14:21

3 Answers 3

1
$\begingroup$

Generate 50 random pairs of data

SeedRandom[10];
data = RandomReal[{-10, 10}, {50, 2}];

SortBy the Last element of each pair and then split into the three groups using SplitBy the Last element

data3 = SplitBy[SortBy[data, Last], Abs[Last@#] > 1 &];

ListPlot the three groups of data

ListPlot[data3, PlotStyle -> {Green, Red, Blue}]

enter image description here

$\endgroup$
3
  • $\begingroup$ This breaks with SeedRandom[778]. $\endgroup$ Nov 8, 2017 at 13:09
  • $\begingroup$ @ChrisDegnen - For RandomSeed[778] there is no data in the range -1 < y < 1 so there is no split thereby resulting in a single color. $\endgroup$
    – Bob Hanlon
    Nov 8, 2017 at 14:25
  • 1
    $\begingroup$ Exactly. Everything from -10 to +10 is coloured green, whereas the positive data points should be blue. $\endgroup$ Nov 8, 2017 at 14:29
0
$\begingroup$
rand = RandomReal[{-10, 10}, RandomInteger[{10, 50}]];
bluelist = redlist = greenlist = {};
Which[1 <= # <= 10, AppendTo[bluelist, #],
     -1 <  # <   1, AppendTo[redlist, #],
    -10 <= # <= -1, AppendTo[greenlist, #]] & /@ rand;
ListPlot[#1, PlotStyle -> #2] & @@
 Transpose[DeleteCases[
   {{bluelist, Blue}, {redlist, Red}, {greenlist, Green}}, {{}, _}]]

enter image description here

$\endgroup$
0
$\begingroup$

Does this help?:

SeedRandom[10, Method -> "MKL"];
data = Transpose[{RandomReal[{-10, 10}, 50],RandomInteger[{-10, 10}, 50]}]; 
colors = Piecewise[{{RGBColor[1, 0, 0], 1 <= #[[1]] <= 10}, 
{RGBColor[0, 1, 0], -0.99 <= #[[1]] <= 0.99},
{RGBColor[0, 0, 1], -10 <= #[[1]] <= -1}}] & /@ data; 
Graphics[{PointSize[0.02], Point[data, VertexColors -> colors]}, 
AspectRatio -> 1, Frame -> True]

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.