5
$\begingroup$

I am looking for a way to align two multiple legends while I can control the position of one of the legend myself.

Consider the following code.

Show[ Plot[{ x, x^2}, {x, 1, 2}
  , Frame -> True
  , PlotLegends -> Placed[ {"A", "B"}, Scaled[{0.7, 0.5}]]
 ], 
ListPlot[ Table[ {i, i}, {i, 0, 2, 0.1}]
  , Frame -> True
  , PlotLegends -> {"C"}
 ]
]

There are two plots with legends and one of the legends are placed manually via Placed. My goal is to make second legend aligned with the first one (possibly placed below) without manual controlling.

I found several related questions but they do not seem to work properly. For example, using the method introduced by ubpdqn of the following question Legend in a Show , one can write the following code.

legended = {LineLegend[Range[2] // Map@ColorData[97] , {"Aa", "Bb"}], 
   PointLegend[{ColorData[97][1]}, {"C"}],
   } // Column
Legended[Show[ Plot[{ x, x^2}, {x, 1, 2}
   , Frame -> True
  ], 
   ListPlot[ Table[ {i, i}, {i, 0, 2, 0.1}]
   , Frame -> True
  ] 
] , Placed[legended, Scaled[{0.3, 0.6}]]]

However, the resulting plot was not quite satisfactory since the size of the legends are inconsistent. Could you suggest better methods for this goal?

$\endgroup$
  • 1
    $\begingroup$ related: adjust the alignment of different legends $\endgroup$ – kglr Jun 11 '15 at 11:20
  • $\begingroup$ @kguler Thanks for the link. It seems that this post is actually duplicated. As Chris Degnen's solution is also valid(and different) approach, I will leave it as it is at this moment. $\endgroup$ – Sungmin Jun 11 '15 at 11:28
6
$\begingroup$

You can do this with the old-style plot legends package.

Quiet@Needs["PlotLegends`"];

ShowLegend[Show[Plot[{x, x^2}, {x, 1, 2}, Frame -> True],
  ListPlot[Table[{i, i}, {i, 0, 2, 0.1}], Frame -> True]],
 {{{Graphics[{ColorData[97, 1], Thick, Line[{{0, 0}, {2, 0}}]}],
    Style["Aa", 12, FontFamily -> "Helvetica"]},
   {Graphics[{ColorData[97, 2], Thick, Line[{{0, 0}, {2, 0}}]}],
    Style["Bb", 12, FontFamily -> "Helvetica"]},
   {Graphics[{ColorData[97, 1], PointSize[0.25], Point[{0, 0}]}],
    Style["C", 12, FontFamily -> "Helvetica"]}}, LegendPosition -> {-0.4, 0},
  LegendSize -> {0.4, 0.4}, LegendShadow -> False, LegendTextSpace -> 1,
  LegendBorder -> None}]

enter image description here

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.