14
$\begingroup$

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.

$\endgroup$

5 Answers 5

14
$\begingroup$

Something like :

Needs["PlotLegends`"]

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"}]

Example

$\endgroup$
3
  • $\begingroup$ Thanks. This helps but I see on the x axis the dashed curve from 0 to pi/2, although it is not easily visible. $\endgroup$ Jun 14, 2012 at 22:00
  • 3
    $\begingroup$ @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]}]. $\endgroup$ Jun 14, 2012 at 22:52
  • $\begingroup$ @J.M.: Thanks everybody. That solves this problem for me. And it was quite simple. $\endgroup$ Jun 15, 2012 at 0:55
9
$\begingroup$

Yet another alternative: Some combination of Mesh and MeshShading

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

enter image description here

To add legends:

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

enter image description here

$\endgroup$
1
  • $\begingroup$ Nice use of mesh. +1 $\endgroup$
    – Mr.Wizard
    Jun 17, 2012 at 15:16
8
$\begingroup$
ShowLegend[
 Show[
  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

$\endgroup$
5
  • $\begingroup$ I like this solution. You can delete it again if you want to but I'll just post it as my own. $\endgroup$
    – Mr.Wizard
    Jun 14, 2012 at 21:37
  • $\begingroup$ 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. $\endgroup$ Jun 15, 2012 at 8:46
  • $\begingroup$ Please see my update. If you feel it is still no good do as you will. $\endgroup$
    – Mr.Wizard
    Jun 15, 2012 at 12:46
  • $\begingroup$ @Mr.Wizard Thanks, after your additions I think it fully answers the OP. $\endgroup$ Jun 15, 2012 at 14:47
  • $\begingroup$ @Mr.Wizard: Thanks. I was also interested in how to also do this with Show. $\endgroup$ Jun 15, 2012 at 19:32
3
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$ Jun 15, 2012 at 12:55
  • $\begingroup$ @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. $\endgroup$ Jun 15, 2012 at 13:06
  • $\begingroup$ Oh yeah, I meant to take the square root above. Thanks. $\endgroup$ Jun 15, 2012 at 14:14
3
$\begingroup$

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

$\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.