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?
3 Answers
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}]
-
-
$\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$ 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
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}}, {{}, _}]]
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]
Select[]
? $\endgroup$ColorFunction
but I see nothing in the Help. Anyone? $\endgroup$ListPlot
, "ColorFunction
requires at least one dataset to beJoined
", i.e., it will vary the color of a line not points. $\endgroup$