2
$\begingroup$

How can I make a background for legend like this?
It's not necessary to be exact the same but something similar that fits the content is good. Or please suggest if you have any idea to make it look better.
I'm going to put it inside some plot with grids that is why I need the background to make it better.
I also need to make the background fits the content.

enter image description here

ClearAll[legend1]
Options[legend1] = {Joined -> Automatic};
legend1[colors_, polymarkers_, legend__, , OptionsPattern[]] := 
 PointLegend[colors, legend, Joined -> OptionValue[Joined], 
  LegendMarkers -> (ResourceFunction["PolygonMarker"][#, Offset[4], 
       EdgeForm[]] & /@ polymarkers), LegendMarkerSize -> {30, 30}, 
  LegendLayout -> (Column[
      Grid[{##}, Alignment -> {Center, Center}] & /@ #, 
      Spacings -> -1] &)]
colors = ColorData[97, #] & /@ Range[6];
polymarkers = {"DownTriangle", "SevenPointedStar", "FourPointedStar", 
   "Diamond"};
mylegend = 
  MaTeX[TeXForm[#], Magnification -> 0.5] & /@ {{"5X", "6Y", "8Z", 
     "9M", "2K", "2N", "2T", "3N", "5O"}, {"1X", "3Y", "5Z", "2L", 
     "9K", "3Z", "7X", "3P", "2T"}, {"3K", "6M"}, {"3H", "6K"}, {"X4",
      "Y5", "C6", "1K"}, {"X6", "Y6", "C6", "Z6"}};
legend1[colors, polymarkers, mylegend, , Joined -> True]

EDIT: For reference, I'll put the legend in a plot like this. enter image description here

Legended[Plot[{x, 2 x - 1.5, x - 1, 1 - x, x - 2, 1/2 x - 1}, {x, 1, 
   5}, GridLines -> {Range[1, 5, 1], Range[1, 5, 1]},
  PlotRange -> {{1, 5}, {0, 5}},ImageSize -> 360], 
 Placed[legend1[colors, polymarkers, mylegend, , 
   Joined -> True ], {0.2, 0.8}]] 
$\endgroup$
4
  • 1
    $\begingroup$ Since you also ask for our opinion: Personally, I wouldn't use a background that fits the content. What about: PointLegend[ ..., LegendFunction -> "Panel"]? $\endgroup$
    – Domen
    Commented Jul 4, 2022 at 22:04
  • $\begingroup$ @Domen I don't have much space to put a rectagular panel like that as I showed with the plot above where the legend is placed on. If you put the rectangular panel it would hide my lines which is not desired. $\endgroup$
    – hana
    Commented Jul 4, 2022 at 22:11
  • $\begingroup$ @Domen after all, I have to accept that no background looks better but it's also good to know how to make background. $\endgroup$
    – hana
    Commented Jul 5, 2022 at 12:10
  • 1
    $\begingroup$ Glad to hear we've reached an aesthetic consensus :-) I still think "Panel" might look the best, together with fiddling the position of the legend or the aspect ratio of the plot or the plot range ... to get it not to overlap with the curves. $\endgroup$
    – Domen
    Commented Jul 5, 2022 at 12:26

2 Answers 2

8
$\begingroup$

Set background color to Row inside LegendLayout:

LegendLayout -> (Column[Row[##, Alignment -> {Center, Center}, 
       Background -> LightGray] & /@ #, Spacings -> -1] &)

enter image description here

$\endgroup$
7
  • $\begingroup$ Is it possible to make straight lines between 1 and 2, 3 and 4, 5 and 6 lines? That would make it much nicer. $\endgroup$
    – hana
    Commented Jul 4, 2022 at 23:01
  • $\begingroup$ Use Partition to group legend elements by 2: LegendLayout -> (Column[Row[{Grid[##, Spacings -> {1, -1}, Alignment -> {Left, Left}]}, Background -> LightGray] & /@ Partition[#, 2], Spacings -> -1] &)] $\endgroup$
    – Domen
    Commented Jul 4, 2022 at 23:14
  • $\begingroup$ I just tried this after a long running like 20 minutes. The plot seems ok but there are two problems with it. 1. It removes the center layout of the text and the marker that I have before. 2. How can I remove the bold outline? $\endgroup$
    – hana
    Commented Jul 5, 2022 at 1:26
  • $\begingroup$ @hana, I don't quite understand your two problems. Can you please post a screenshot of the result and show where the problems are? $\endgroup$
    – Domen
    Commented Jul 5, 2022 at 10:58
  • 1
    $\begingroup$ Don't use opacity (if you don't really need it). Use Background -> GrayLevel[.95] to get a lighter gray. $\endgroup$
    – Domen
    Commented Jul 5, 2022 at 11:18
2
$\begingroup$

Add Dividers?

LegendLayout -> (Column[
    Grid[{##}, Alignment -> {Center, Center}, 
       Background -> LightGray] & /@ #, Spacings -> -1, 
    Dividers -> {None, {3 -> Blue, -3 -> Blue}}] &)

enter image description here

Or just ?

LegendLayout -> (Column[
    Grid[{##}, Alignment -> {Center, Center}] & /@ #, Spacings -> -1, 
    Dividers -> {None, {3 -> Blue, -3 -> Blue}}, 
    Background -> LightGray] &)
$\endgroup$
3
  • $\begingroup$ Would it possible to specify the shape myselft with polygon? I don't want to group pairs like that as they are not supposed to grouped. $\endgroup$
    – hana
    Commented Jul 5, 2022 at 0:03
  • $\begingroup$ @hana Maybe use the setting by @Domen LegendLayout -> (Column[ Row[{Grid[##, Spacings -> {1, -1}, Alignment -> {Left, Left}, Dividers -> {{-1 -> True}, {-1 -> True}}]}, Background -> LightGray] & /@ Partition[#, 2], Spacings -> .03] &) $\endgroup$
    – cvgmt
    Commented Jul 5, 2022 at 0:40
  • $\begingroup$ That seems ok but there are two problems with it. 1. It removes the center layout of the text and the marker that I have before. 2. How can I remove the bold outline? $\endgroup$
    – hana
    Commented Jul 5, 2022 at 1:23

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.