0
$\begingroup$

In a previous post, we saw how to change the colors in different segments of the ListPLot when the intervals on the $x$ axis are given.

My question now is the following:

For the same test data how to change the color of the line in different segments according to the value of $y$. For example:

 data = {{-3, -1}, {-2, -1}, {-1, -1}, {-0.2, -0.2}, {0, 0}, {0.5, 0.5},
        {2, 2}, {3, 2}, {4, 2}};
  • for $-0.92 < y < -0.5$ ---> red thick
  • for $0.12 < y < 0.94$ ---> blue thick
  • for $1.32 < y < 1.45$ ---> 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 y 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?

$\endgroup$
2
  • 2
    $\begingroup$ You can still use kglr's answer: add the setting MeshFunctions -> {#2 &} and make the necessary adjustments in the Mesh setting. $\endgroup$ Oct 11, 2016 at 9:10
  • $\begingroup$ @J.M. You are a life saver! Please post a quick reply, so as to accept it. $\endgroup$
    – Vaggelis_Z
    Oct 11, 2016 at 9:14

1 Answer 1

3
$\begingroup$

You can use MeshShading, e.g.

ListLinePlot[data, MeshFunctions -> (#2 &), 
 Mesh -> {{-0.92, -0.5, 0.12, 0.94, 1.32, 1.45}}, MeshStyle -> None, 
 MeshShading -> {Black, Red, Black, Blue, Black, Green, Black}, 
 PlotStyle -> Thickness[0.02]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.