11
$\begingroup$

Kind of as an extension of this question: How to plot a series using different colors depending on the y-value

Is there something similar to MeshFunction or ColorFunction that can control whether a curve being plotted is dashed vs solid depending on the y-value?

$\endgroup$
12
$\begingroup$

A simple modification of the answer in the linked Q/A:

data = RandomReal[{0, 3}, 100];
ListLinePlot[data, MeshFunctions -> {#2 &}, Mesh -> {{1, 2}}, 
 MeshShading -> {Blue, Directive[Red, Dashed], Green}, MeshStyle -> None]

enter image description here

Use MeshShading -> {Dashing[.02], Dashing[None], Dotted} to get

enter image description here

$\endgroup$
  • $\begingroup$ Thanks so much for the fast answer! Just a quick question about how to apply it. If I want to dash when the absolute value is greater than 1, this code produces weird results. Any idea? data = RandomReal[{-3, 3}, 100]; ListLinePlot[data, MeshFunctions -> {Abs[#2] &}, Mesh -> {{1}}, MeshShading -> {Blue, Directive[Red, Dashed]}, MeshStyle -> None] $\endgroup$ – Hanmyo Feb 6 '15 at 8:45
  • $\begingroup$ @Hanmyo, i am getting the same "weird results" with Abs[#2]&. I don't know why; maybe a bug? Meanwhile, a work-around is to use ListLinePlot[data, MeshFunctions -> {#2 &}, Mesh -> {{-1, 1}}, MeshShading -> {Blue, Directive[Red, Dashed]}, MeshStyle -> None]. Btw, thank you for the accept. $\endgroup$ – kglr Feb 6 '15 at 13:27
  • $\begingroup$ Thanks for the response. I think I'll post it as a separate question, as I'm looking to do something more complicated involving Abs. $\endgroup$ – Hanmyo Feb 6 '15 at 20:34

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.