Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here is the motivation for my question. I want to plot Sin[x] over the interval 0 to 2 π, with the part from π/2 to 2 π as a dashed curve. I want to plot this with a legend so that it shows the solid part is for acute angles, and the dashed part is for, say, other angles. Perhaps using Show and ShowLegend will work, but I couldn't figure out how to get the legend boxes to match the curves (solid and dashed curves). It seems PlotLegend takes care of this for you, but I couldn't figure out how to use this in Show. So I thought using PlotLegend in Plot will be easiest, except I don't know how to use Plot for the same function with different options on different intervals.

I tried using the suggestions from Plotting piecewise function with distinct colors in each section, but I think my problem is that the same function is being used for both parts of the piecewise function I defined.

share|improve this question
up vote 11 down vote accepted

Something like :


Plot[{Piecewise[{{Sin[x], 0 <= x <= \[Pi]/2}}, 0], Piecewise[{{Sin[x], \[Pi]/2 <= x <= 2 \[Pi]}}, 0]}, {x, 0,  2 \[Pi]}, PlotStyle -> {Black, Dashed},PlotLegend -> {"Acute angles", "Other angles"}]


share|improve this answer
Thanks. This helps but I see on the x axis the dashed curve from 0 to pi/2, although it is not easily visible. – Ben Allgeier Jun 14 '12 at 22:00
@Ben: It's easily fixed; have the default value of Piecewise[] be Indeterminate instead of 0; thus, Plot[{Piecewise[{{Sin[x], 0 <= x <= Pi/2}}, Indeterminate], Piecewise[{{Sin[x], Pi/2 <= x <= 2 Pi}}, Indeterminate]}, {x, 0, 2 Pi}, PlotStyle -> {Black, Directive[Black, Dashed]}]. – J. M. Jun 14 '12 at 22:52
@J.M.: Thanks everybody. That solves this problem for me. And it was quite simple. – Ben Allgeier Jun 15 '12 at 0:55
  Plot[Sin[x], {x, 0, \[Pi]/2}, PlotStyle -> Dashing[None],
  Ticks -> {{0, \[Pi]/2, \[Pi], (3 \[Pi])/2, 2 Pi}, Automatic}], 
  Plot[Sin[x], {x, \[Pi]/2, 2 \[Pi]}, PlotStyle -> Dashing[Tiny],
  Ticks -> {{0, \[Pi]/2, \[Pi], (3 \[Pi])/2, 2 Pi}, Automatic}], 
  PlotRange -> All
  {Graphics[{ColorData[1][1], ##2, Line[{{0, 0}, {2, 0}}]}], #} &
    @@@ {{"Acute \[Angle]"}, {"Other \[Angle]", Dashed}}

Mathematica graphics

share|improve this answer
I like this solution. You can delete it again if you want to but I'll just post it as my own. – Mr.Wizard Jun 14 '12 at 21:37
I deleted it because in re-reading the OP I realised that it didn't actually answer the question, as they had had exactly the same problem as I encountered. Namely that I didn't seem to be able to get PlotLegend to work with Show. – image_doctor Jun 15 '12 at 8:46
Please see my update. If you feel it is still no good do as you will. – Mr.Wizard Jun 15 '12 at 12:46
@Mr.Wizard Thanks, after your additions I think it fully answers the OP. – image_doctor Jun 15 '12 at 14:47
@Mr.Wizard: Thanks. I was also interested in how to also do this with Show. – Ben Allgeier Jun 15 '12 at 19:32

Yet another alternative: Some combination of MeshFunctions and MeshShadingas in

Plot[Sin[x], {x, 0, 2 \[Pi]}, PlotStyle -> Red,
 Ticks -> {{0, \[Pi]/2, \[Pi], (3 \[Pi])/2, 2 Pi}, Automatic}, 
 MeshFunctions -> {Boole[# >= Pi/2] &}, Mesh -> {{0}}, 
 MeshStyle -> None, 
 MeshShading -> {Directive@{Thick, Dashing[Tiny], Green},  Directive@{Dashed, Red}}]

enter image description here

To add legends:

Plot[Sin[x], {x, 0, 2 \[Pi]}, PlotStyle -> Red, 
 Ticks -> {{0, \[Pi]/2, \[Pi], (3 \[Pi])/2, 2 Pi}, Automatic}, 
 MeshFunctions -> {Boole[# >= Pi/2] &}, Mesh -> {{0}}, 
 MeshStyle -> None, 
 MeshShading -> {Directive@{Thick, Dashing[Tiny], Green}, Directive@{Dashed, Red}}, 
 Epilog ->  Inset[Panel@
 Grid[{{Graphics[{Thick, Green, Dashing[Tiny], 
    Line[{{0, 0}, {1, 0}}]}, AspectRatio -> .1, ImageSize -> 30],
    Style["x <= \[Pi]/2 ", 12, 
    Green]}, {Graphics[{Dashed, Red, Line[{{0, 0}, {1, 0}}]}, 
    AspectRatio -> .1, ImageSize -> 30], 
    Style["x >= \[Pi]/2 ", 12, Red]}}], 
 Offset[{-10, -10}, Scaled[{1, 1}]], {Right, Top}]]

enter image description here

share|improve this answer
Nice use of mesh. +1 – Mr.Wizard Jun 17 '12 at 15:16

Very dirty trick to enable you to use the methods in this question:

plt = Plot[Piecewise[{{Sqrt[Haversine[2 x]], 0 <= x <= Pi/2},
           {Sin[x], Pi/2 < x}}], {x, 0, 2 Pi}];
li = Cases[plt, _Line, Infinity];
Graphics[Transpose[{{Dashing[None], Dashing[Small]}, li}], 
 AspectRatio -> OptionValue[Plot, AspectRatio], Axes -> True]

As for the legend... you could do what b.gatessucks did, but I'm told there are better options. Search around the site for stuff on legends.

share|improve this answer
So what really makes this work? Mathematically, Haversine[2x] is equal to Sin[x] on the interval [0, Pi/2]. It looks like plt has the form {Line[], Line[] }. So the plot thinks of it as two different functions still. Is this right? Interesting solution. – Ben Allgeier Jun 15 '12 at 12:55
@Ben, no; $\mathrm{hav}(x)=\sin^2\dfrac{x}{2}$. Otherwise, yes; I'm exploiting the fact that $\mathrm{hav}(x)$ is not immediately simplified to more basic trigonometric functions. – J. M. Jun 15 '12 at 13:06
Oh yeah, I meant to take the square root above. Thanks. – Ben Allgeier Jun 15 '12 at 14:14

A more generalized version of b.gatessucks' answer:

xmin = 0; xmax = 2*Pi;
range = {xmin, Pi/2, xmax};
intervals[x_, range_] := LessEqual @@@ (Insert[#, x, 2] & /@ 
                           Delete[Thread[{range, RotateLeft@range}], -1])
fun[x_] := Sin@x
Plot[Evaluate@(Piecewise[{{fun[x], #}}, Indeterminate] & /@ intervals[x, range]), 
  {x, xmin, xmax}, 
  PlotStyle -> {{Red, Dashed}, {Black, Dotted}}, 
  Ticks -> {Range[xmin, xmax, Pi/2], Automatic}]

enter image description here

Then, one can more easily do this kind of plot:

enter image description here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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