14

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.

kglr
  • 394,356
  • 18
  • 477
  • 896
Ben Allgeier
  • 367
  • 2
  • 7

5 Answers5

14

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

b.gates.you.know.what
  • 20,103
  • 2
  • 43
  • 84
  • 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
  • 3
    @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.'s missing motivation 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 00:55
9

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

kglr
  • 394,356
  • 18
  • 477
  • 896
8
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

image_doctor
  • 10,234
  • 23
  • 40
3

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

Öskå
  • 8,587
  • 4
  • 30
  • 49
3

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.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
  • 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.'s missing motivation 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