1

I was trying to include the legends inside the frame of a plot and I did some research, but I still couldn't get it done. The error shows "An improperly formatted option was encountered while reading a GraphicsBox. The option was not in the form of a rule." and "Coordinate {Skeleton[2]} should be a pair of numbers, or a Scaled or Offset form."

below is what I did:

Show[[ListPlot[Slopetriangle, PlotStyle -> {Black}, Joined -> True], 
  PlotEMF2a, PlotEMF2b, PlotEMF2c, PlotEMF2d, 
   PlotRange -> {{-12, 3}, All}, AxesOrigin -> {-12, -350}, 
  Frame -> True, LabelStyle -> {Black, Bold}, 
  FrameLabel -> {"log \!\(\*SubscriptBox[\(a\), \
SuperscriptBox[\(Js\), \(+\)]]\)", "EMF (mV)"}, Background -> White, 
  PlotLegends -> 
   Placed[
    LineLegend[{"C6", "C8", "C10", "C12"}, LegendLayout -> {"Row",4}],
     PlotRange -> {{-12, 3}, {-350, 100}}, Axes -> False]]]

zoey
  • 105
  • 6
  • Without definitions we cannot reproduce your results. However, Show should only use single brackets ([ ]) rather than double brackets ([[ ]]). – Bob Hanlon Jun 22 '21 at 19:49
  • I changed the [[ ]] to [ ], now I can see the figure and the legends but the legends are still out side the plot frame. Any suggestions? thank you – zoey Jun 22 '21 at 19:58
  • Include the code for all of the definitions for a minimal example that demonstrates your problem. – Bob Hanlon Jun 22 '21 at 20:00
  • Show[ListPlot[Slopetriangle1, PlotStyle -> {Black}, Joined -> True], ListPlot[Slopetriangle2, PlotStyle -> {Black, Dashing[{0.05, 0.01}]}, Joined -> True], PlotRange -> {{-12, 3}, All}, AxesOrigin -> {-12, -350}, Frame -> True, LabelStyle -> {Black, Bold}, FrameLabel -> {"log !(*SubscriptBox[(a), SuperscriptBox[(Js), \(+)]])", "EMF (mV)"}, Background -> White, PlotRange -> {{-12, 3}, {-350, 100}}, Axes -> False, PlotLegends -> Placed[ LineLegend[{"triangle1", "triangle2"}, LegendLayout -> {"Row", 2}, {Left, Top}], PlotRange -> {{-12, 3}, {-350, 100}}, Axes -> False]] – zoey Jun 22 '21 at 20:15
  • Slopetriangle1 = {{1.8, -250}, {1.8, -250 + 59 + 59}, {-0.2, -250}, {1.8, -250}} Slopetriangle2 = {{1.8, -250}, {1.8, -250 + 59 + 59 + 59}, {-0.2, -250}, {1.8, -250}} – zoey Jun 22 '21 at 20:15

2 Answers2

2
Clear["Global`*"]

Slopetriangle1 = {{1.8, -250}, {1.8, -250 + 59 + 
    59}, {-0.2, -250}, {1.8, -250}}; Slopetriangle2 = {{1.8, -250}, {1.8, \
-250 + 59 + 59 + 59}, {-0.2, -250}, {1.8, -250}};

The easiest solution is to combine into a single ListPlot

ListPlot[{Slopetriangle1, Slopetriangle2},
 PlotStyle -> {Black, {Black, Dashing[{0.05, 0.01}]}},
 Joined -> True,
 PlotRange -> {{-12, 3}, {-260, -50}},
 Frame -> True,
 Axes -> False,
 LabelStyle -> {Black, Bold},
 PlotLegends -> Placed[
   LineLegend[{"triangle1", "triangle2"}],
   {Left, Top}]]

enter image description here

Or since PlotLegends is not an option for Show, use Legended

Legended[
 Show[
  ListPlot[Slopetriangle1,
   PlotStyle -> Black,
   Joined -> True],
  ListPlot[Slopetriangle2,
   PlotStyle -> {Black, Dashing[{0.05, 0.01}]},
   Joined -> True],
  PlotRange -> {{-12, 3}, {-260, -50}},
  Frame -> True,
  Axes -> False,
  LabelStyle -> {Black, Bold}],
 Placed[
  LineLegend[{Black, {Black, Dashing[{0.05, 0.01}]}},
   {"triangle1", "triangle2"}],
  {.175, .85}]]

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
1

One way to do it is to wrap the Legended function around Show. Here is an example:

data = Table[{x, Sin[x]}, {x, 0, 2 \[Pi], \[Pi]/10}];
lgnd = Framed[
   Column[{
     PointLegend[{Red}, {"data"}],
     LineLegend[{Blue}, {"fit"}]}], RoundingRadius -> 20, 
   Background -> LightBlue];
Legended[
 Show[ListPlot[data, PlotStyle -> Red], Plot[Sin[x], {x, 0, 2 \[Pi]}],
   Frame -> True, LabelStyle -> {Black, Bold}, 
  FrameLabel -> {"log \!\(\*SubscriptBox[\(a\), \
SuperscriptBox[\(Js\), \(+\)]]\)", "EMF (mV)"}, Background -> White], 
 Placed[lgnd, {0.75, 0.8}]]

enter image description here

Jack McInerney
  • 395
  • 1
  • 10
  • it somehow works, but for the LineLegend[{Blue}, {"fit"}], if I have four lines that are with PlotStyle -> {Black, Dashing[None]}; {Black, Dashing[{0.05, 0.01}]}; {Black, Dashing[{0.03, 0.01}]}; and {Black, Dashing[{0.01, 0.01}]}, how can do with these four line legends? – zoey Jun 22 '21 at 20:49
  • The variable lgnd is a list of the items in your legend. You can simply add to the list, with each item looking something like: LineLegend[{Black}, {"label"}]. I don't know how to add the dashing. – Jack McInerney Jun 22 '21 at 21:22
  • I just edited my answer to adding the Dashing. – Jack McInerney Jun 22 '21 at 21:35
  • I've got it fixed. Thanks for the help – zoey Jun 22 '21 at 22:10