0
$\begingroup$

I have two graphs:

Plot[{x, x^2, x^3}, {x, 0, 3}, PlotRange -> {0, 27}, 
 AxesOrigin -> {0, 0}, AspectRatio -> 0.75, 
 PlotStyle -> {{Black}, {Black, Dotted}, Black}, 
 AxesLabel -> {Style["x", 18], Style["y", 18]}, LabelStyle -> {13}, 
 PlotLegends -> {"func. 1", "func. 2", "func. 3"}]

enter image description here

Show[
 Plot[x, {x, 0, 3}, PlotRange -> {0, 27}, AxesOrigin -> {0, 0}, 
  AspectRatio -> 0.75, PlotStyle -> {Black}, 
  AxesLabel -> {Style["x", 18], Style["y", 18]}, LabelStyle -> {13}, 
  PlotLegends -> {"func. 1"}],
 Plot[x^2, {x, 0, 3}, PlotRange -> {0, 27}, AxesOrigin -> {0, 0}, 
  AspectRatio -> 0.75, PlotStyle -> {Black, Dotted}, 
  AxesLabel -> {Style["x", 18], Style["y", 18]}, LabelStyle -> {13}, 
  PlotLegends -> {"func. 2"}],
 Plot[x^3, {x, 0, 3}, PlotRange -> {0, 27}, AxesOrigin -> {0, 0}, 
  AspectRatio -> 0.75, PlotStyle -> {Black}, 
  AxesLabel -> {Style["x", 18], Style["y", 18]}, LabelStyle -> {13}, 
  PlotLegends -> {"func. 3"}]]

enter image description here

I would like to do the legends on the second graphs the same as on the first one. It's clear that they have different vertical shift and the line length. So, I would like to do the second legends identical to the first one if possible, of course :)

$\endgroup$

1 Answer 1

1
$\begingroup$

Use LegendMarkerSize in conjunction with a LineLegend.

Show[
 Plot[x^3, {x, 0, 3}, PlotRange -> {0, 27}, AxesOrigin -> {0, 0}, 
  AspectRatio -> 0.75, PlotStyle -> {Black, Dashed}, 
  AxesLabel -> {Style["x", 18], Style["y", 18]}, LabelStyle -> {13}, 
  PlotLegends -> 
   Placed[LineLegend[{Directive[Black, Thin, Dashed]}, {"func. 1"}, 
     LegendMarkerSize -> 20], After]]
 , Plot[x^2, {x, 0, 3}, PlotRange -> {0, 27}, AxesOrigin -> {0, 0}, 
  AspectRatio -> 0.75, PlotStyle -> {Black, Dotted}, 
  AxesLabel -> {Style["x", 18], Style["y", 18]}, LabelStyle -> {13}, 
  PlotLegends -> 
   Placed[LineLegend[{Directive[Black, Dotted]}, {"func. 2"}, 
     LegendMarkerSize -> 20], After]]
 , Plot[x, {x, 0, 3}, PlotRange -> {0, 27}, AxesOrigin -> {0, 0}, 
  AspectRatio -> 0.75, PlotStyle -> {Black, Thin}, 
  AxesLabel -> {Style["x", 18], Style["y", 18]}, LabelStyle -> {13}, 
  PlotLegends -> 
   Placed[LineLegend[{Directive[Black, Thin]}, {"func. 3"}, 
     LegendMarkerSize -> 20], After]]
 ]

enter image description here

I have renamed functions to ensure that the legend corresponds to the order in which the curves appear. However, a better way would be to combine plots and legends inside a Plot command and then to use the LegendLayout function as shown in 256344.

EDIT

To control the spacing: use {1, 0.7}, {1, 0.65}, {1, 0.6} as an example instead of the After in the plots, respectively.

$\endgroup$
1
  • 1
    $\begingroup$ Thanks so much. Your example with LegendMarkerSize and LineLegend is exactly what I need. I will use it in both cases. $\endgroup$ Jun 2, 2022 at 12:42

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.