Let's define some sample data
data = {{-3, -1}, {-2, -1}, {-1, -1}, {-0.2, -0.2}, {0, 0}, {0.5, 0.5},
{2, 2}, {3, 2}, {4, 2}};
and create the corresponding list plot
L0 = ListPlot[data, Joined -> True, PlotStyle -> {Black, Thick}]
Now, I want the following: change the color at different segments of the thick line. For example:
- for $-2.4 < x < -1.1$ ---> red thick
- for $-0.2 < x < 0.94$ ---> blue thick
- for $2.22 < x < 3.4$ ---> green thick
- in all other cases ---> black thick
IMPORTANT NOTE: The real data contain thousands of pairs which cannot be separated into sections. Only the intervals on the x axis are known. So, I want a continuous line with different colors in each segment.
What would be an elegant and quick way to do this?
Many thanks in advance!
ListPlot[sectioned, PlotStyle -> {Directive[Thick, Blue], Directive[Thick, Blue], Directive[Thick, Green], Directive[Thick, Black]}]
where the styles inPlotStyle
are in the correct order. $\endgroup$ColorFunction
andPiecewise[]
. $\endgroup$Mesh
solutions propose below. Far simpler. $\endgroup$