5
$\begingroup$

I used this link: Plot that draws a dashed/solid curve depending on the y-value of the curve to help me start this. I have two lines. I want to change from solid to dashed after an intersection. The line with higher slope will have dashed line AFTER the intersection and the line with lower slope will have dashed line BEFORE the intersection. These two lines should be red and blue.

Here is what I have done so far...

In[289]:= y1 = 1.44; y2 = 27.9 - 16000 x;
intercept = x /. Solve[y1 == y2, x][[1]]

Out[290]= 0.00165375

The plot...

Plot[{y1, y2}, {x, .0014, .0019}, PlotRange -> All, 
 MeshFunctions -> {#1 &}, 
 Mesh -> {{0.0014, intercept}, {intercept, 0.0019}}, 
 MeshShading -> {Blue, Directive[Blue, Dashed]}, MeshStyle -> None]

Here was the result...

enter image description here

Not sure how to fix this. Please help me.

$\endgroup$
0

3 Answers 3

4
$\begingroup$

If you're not wed to Mesh, it might be easier for you to accomplish what you're looking for with just having multiple functions, ala:

Plot[{ If[intercept > x, y1, None], If[intercept < x, y1, None],
    If[intercept > x, y2, None], If[intercept < x, y2, None]} // 
  Evaluate, {x, .0014, .0019}, PlotRange -> All, 
 PlotStyle -> {{Blue, Dashed}, {Blue}, {Red}, {Red, Dashed}}, 
 Axes -> False, Frame -> True, 
 Epilog -> {PointSize[1/40], Gray, Point[{intercept, y1}]}]

yielding

enter image description here

(if you hate the dot get rid of the epilog)


Update riffing off your previous question re: Callouts, can combine as, e.g.

{f1, f2, f3, f4} = { If[intercept > x, y1, None], 
   If[intercept < x, y1, None],
    If[intercept > x, y2, None], If[intercept < x, y2, None]};

xRange = {.0014, .0019}; yRange = {-3, 8};

Plot[ {Callout[f1, "labelY1", {0.001775`, 3.64`}, {0.00174`, 1.754`}],
    f2, f3, 
   Callout[f4, "labelY2", {0.001525`, 5.7`}, {0.00156`, 3.3142`}]} // 
  Evaluate, Prepend[x][xRange], 
 PlotStyle -> {{Blue, Dashed}, {Blue}, {Red}, {Red, Dashed}}, 
 PlotRange -> yRange, Axes -> False, Frame -> True]

yielding:

enter image description here

$\endgroup$
3
  • $\begingroup$ So here, LabelY1 and LabelY2 cannot have assigned values with units. Which, would go back to my original problem in that question $\endgroup$
    – SarahM
    Commented Sep 13, 2017 at 23:18
  • 1
    $\begingroup$ I just used the strings "LabelY1" and "LabelY2" to not overly import from the other question. Can completely use e.g.: Plot[ {Callout[f1, formatQ["operational", q1], {0.001775, 3.64}, {0.00174,1.754}], f2, f3, Callout[f4, formatQ["accelerated", q2], {0.001525, 5.7}, {0.00156, 3.3142}]} // Evaluate, Prepend[x][xRange], PlotStyle -> {{Blue, Dashed}, {Blue}, {Red}, {Red, Dashed}}, PlotRange -> yRange, Axes -> False, Frame -> True] where formatQ provides formatting based upon data in q1 and q2. $\endgroup$ Commented Sep 13, 2017 at 23:22
  • $\begingroup$ ok, I see what you mean now. That works very well. $\endgroup$
    – SarahM
    Commented Sep 13, 2017 at 23:25
3
$\begingroup$
Show[
 Plot[y1, {x, .0014, .0019},
  Mesh -> {{0.0014, intercept}, {intercept, 0.0019}},
  MeshShading -> {Blue, Directive[Blue, Dashed]}],

 Plot[y2, {x, .0014, .0019},
  Mesh -> {{0.0014, intercept}, {intercept, 0.0019}},
  MeshShading -> {Directive[Red, Dashed], Red}],

 PlotRange -> All]

enter image description here

$\endgroup$
1
  • $\begingroup$ Ok, will try that. My actual plot has multiple things going on. I will see if this way interferes with the functions I would like to see on the plot. $\endgroup$
    – SarahM
    Commented Sep 13, 2017 at 22:39
2
$\begingroup$

You have over specified your Mesh option. You only need: Mesh -> {{intercept}}. Unfortunately this solution does use show.

Show[
 Plot[#1, {x, .0014, .0019},
    PlotRange -> All,
    Mesh -> {{intercept}},
    MeshShading -> #2,
    MeshStyle -> None] & @@@
  {
   {y2, {Directive[Blue, Dashed], Blue}},
   {y1, {Red, Directive[Red, Dashed]}}
  }
 ]

Mathematica graphics

Hope this helps.

$\endgroup$
1
  • $\begingroup$ I want the results to look like @eldo answer, but I do not want to use Show $\endgroup$
    – SarahM
    Commented Sep 13, 2017 at 22:52

Your Answer

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

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