SeedRandom[10];
data = RandomReal[{-10, 10}, {50, 2}];
d1 = Select[data, Last@# < -1 &];
d2 = Select[data, -1 <= Last@# <= 1 &];
d3 = Select[data, Last@# > 1 &];
p1 = ListPlot[{d1, d2, d3}
, PlotStyle -> {Darker@Green, Red, Blue}
];
p2 = Plot[{1, -1}, {x, -10, 10}
, PlotStyle -> {None, None}
, PlotRange -> {-12, 12}
, Filling -> {
1 -> {Top, LightBlue}
, 2 -> {Bottom, LightGreen}
, 1 -> {{2}, Nest[Lighter, Red, 6]}
}
];
Show[p2, p1]
EDIT
Using a ColorFunction
is possible for a ListLinePlot
as @BobHanlon mentioned in the comments. Assuming that the data2
is a list of 50
random numbers between {-10,10}
, here are a few variations:
SeedRandom[10];
data2 = RandomReal[{-10, 10}, 50];
If[# < -1, Style[#, Darker@Green],
If[# > 1, Style[#, Blue], Style[#, Red]]] & /@ data2 // Sort
GraphicsRow[ListLinePlot[#
, ColorFunction ->
Function[{x, y}, If[y < -1, Darker@Green, If[y > 1, Blue, Red]]]
, ColorFunctionScaling -> False
, PlotRange -> {-10, 10}
] & /@ {data2, data2 // Sort}
]
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$