3
$\begingroup$

I have two curves that I wanted to plot where each curve is composed of two functions. As an example, the first curve is the piecewise function $\rm{sin(x)}$ for $x \in [0,5]$ and $(x-5)+\rm{sin}(5)$ for $x \in [5,10]$.

My issue is when plotting it, the PlotLegend assigns a shorter line on the first curve (solid line) as opposed to the second curve (dashed line). How to fix this?

In addition, is there a way to place the legend inside the plot, in the lower right corner? It's better if there is a way to input coordinates for the position, say $(8,-2)$, so that it's easily adjustable.

p1 = Plot[Sin[x], {x, 0, 5}, Frame -> True, FrameLabel -> {"x", "y"}, LabelStyle -> Directive[Black, 20], PlotStyle -> {Black, Thickness[thickness]}, PlotPoints -> 500, PlotRange -> {{0, 10}, {-3, 3}}, PlotLegends -> Placed[{"Sin(x)"}, Right], AspectRatio -> 3/4, ImageSize -> Large];
p2 = Plot[(x - 5) + Sin[5], {x, 5, 10}, Frame -> True, FrameLabel -> {"x", "y"}, LabelStyle -> Directive[Black, 20], PlotStyle -> {Black, Thickness[thickness]}, PlotPoints -> 500, PlotRange -> {{0, 10}, {-3, 3}}, AspectRatio -> 3/4, ImageSize -> Large];
p3 = Plot[Cos[x], {x, 0, 5}, Frame -> True, FrameLabel -> {"x", "y"}, LabelStyle -> Directive[Black, 20], PlotStyle -> {Black, Dashed, Thickness[thickness]}, PlotPoints -> 500, PlotRange -> {{0, 10}, {-3, 3}}, PlotLegends -> Placed[{"Cos(x)"}, Right], AspectRatio -> 3/4, ImageSize -> Large];
p4 = Plot[(x - 5) + Cos[5], {x, 5, 10}, Frame -> True, FrameLabel -> {"x", "y"}, LabelStyle -> Directive[Black, 20], PlotStyle -> {Black, Dashed, Thickness[thickness]}, PlotPoints -> 500, PlotRange -> {{0, 10}, {-3, 3}}, AspectRatio -> 3/4, ImageSize -> Large];
Show[p1, p2, p3, p4]

Image

$\endgroup$
5
  • 1
    $\begingroup$ Try e.g.: PlotLegends -> Placed[{"Sin(x)"}, Scaled[{0.8, 0.1}]] $\endgroup$ Apr 23, 2023 at 9:08
  • $\begingroup$ @DanielHuber The positioning worked but the length of the legends are still not the same, the solid line is shorter than the dashed line. $\endgroup$
    – mathemania
    Apr 23, 2023 at 9:39
  • $\begingroup$ Sorry, but I do not know how to change this automatism. $\endgroup$ Apr 23, 2023 at 9:50
  • $\begingroup$ Without PlotStyle->...Dashed.. there is no difference in the length of margins. $\endgroup$ Apr 23, 2023 at 9:55
  • $\begingroup$ PlotLegends -> Placed[LineLegend[{Directive[{Black, Dashed}]}, {"Cos[x]"}, LegendMarkerSize -> 20], Right] and make the same change to the other entry, removing the Dashed directive. $\endgroup$
    – Syed
    Apr 23, 2023 at 13:16

2 Answers 2

4
$\begingroup$

The different lengths of lines in the legend is indeed weird – one might even consider this as a bug.

The problem is most easily solved if you use one Plot instead of combining many of them with Show (which seems to be the cause of troubles). Furthermore, you can use Piecewise to construct your functions.

For better clarity, I have removed options that are not part of the problem. Also, relative positioning of the legend can be done with Placed, if you want to provide absolute coordinates, use Inset.

f1 = Piecewise[{{Sin[x], 0 <= x <= 5}, {Sin[5] + (x - 5), 5 < x <= 10}}];
f2 = Piecewise[{{Cos[x], 0 <= x <= 5}, {Cos[5] + (x - 5), 5 < x <= 10}}];

Plot[{f1, f2}, {x, 0, 10}, PlotStyle -> {Black, {Black, Dashed}}, 
 PlotLegends -> Placed[{"sin(x)", "cos(x)"}, {.2, .8}]]

enter image description here

$\endgroup$
2
$\begingroup$
$Version

(* "13.2.1 for Mac OS X ARM (64-bit) (January 27, 2023)" *)

Clear["Global`*"]

Alternatively, for a fuller description of the curves in the legend, use ConditionalExpression

Plot[{
  ConditionalExpression[Sin[x], 0 <= x <= 5],
  ConditionalExpression[(x - 5) + Sin[5], 5 < x <= 10],
  ConditionalExpression[Cos[x], 0 <= x <= 5],
  ConditionalExpression[(x - 5) + Cos[5], 5 < x <= 10]},
 {x, 0, 10},
 PlotLegends -> Placed["Expressions", {.325, .7}]]

enter image description here

EDIT: For dashed lines

With[{cd = ColorData[97]},
 Plot[{
   ConditionalExpression[Sin[x], 0 <= x <= 5],
   ConditionalExpression[(x - 5) + Sin[5], 5 < x <= 10],
   ConditionalExpression[Cos[x], 0 <= x <= 5],
   ConditionalExpression[(x - 5) + Cos[5], 5 < x <= 10]},
  {x, 0, 10},
  PlotStyle -> {cd[1], cd[2], {cd[3], Dashed}, {cd[4], Dashed}},
  PlotLegends -> Placed["Expressions", {.325, .7}]]]

enter image description here

$\endgroup$

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.