1
$\begingroup$

I'd like to put a frame around the elements of swatch legend:

SwatchLegend[{Red, Green, Blue}, {"label1", "label2", "label3"}, 
 LegendLayout -> "Row"]

That is, not around the entire legend like this:

SwatchLegend[{Red, Green, Blue}, {"label1", "label2", "label3"}, 
 LegendFunction -> "Frame"]

but ONLY around the red, green and blue squares (a separate frame around each square). Is that possible?

$\endgroup$
2
  • $\begingroup$ Around all the squares together, or around each square individually? $\endgroup$
    – Mr.Wizard
    Jul 31, 2017 at 12:46
  • $\begingroup$ @Mr.Wizard: Around each square separately. Sorry, it didn't became clear I corrected it $\endgroup$
    – holistic
    Jul 31, 2017 at 13:05

1 Answer 1

4
$\begingroup$

One method is to use LegendMarkers and draw a frame using an empty Rectangle:

SwatchLegend[{Red, Green, Blue}, {"label1", "label2", "label3"}, 
 LegendFunction -> "Frame", LegendMarkerSize -> 30, 
 LegendMarkers -> 
  Graphics[{Rectangle[], FaceForm[], EdgeForm[Opacity[1]], Rectangle[{-1/2, -1/2}, {3/2, 3/2}]}]]

enter image description here

Another is to use LegendFunction to operate on the contents of the legend, here with MapAt and a rather unwieldy position specification:

SwatchLegend[{Red, Green, Blue}, {"label1", "label2", "label3"}, 
 LegendFunction -> MapAt[Framed, {1, 1, 1, 1, 1, 1, 1, All, 1}]]

enter image description here

From a comment below you may want EdgeForm rather than a separate frame element:

SwatchLegend[{Red, Green, Blue}, {"label1", "label2", "label3"}, 
 LegendFunction -> "Frame", LegendMarkerSize -> 20, 
 LegendMarkers -> Graphics[{EdgeForm[{Thick, Black, Opacity[1]}], Rectangle[]}]]

enter image description here

$\endgroup$
3
  • $\begingroup$ Ah I see,thank you! So the markers were just replaced by rectangles, interesting solution. Is it possible to remove the space between the colored rectangles and the "frame" rectangle and color the frame Black? I tried: Graphics[{Rectangle[], FaceForm[], Black, Rectangle[{-1/2, -1/2}, {3/2, 3/2}]}] but that didn't seem to work $\endgroup$
    – holistic
    Jul 31, 2017 at 13:18
  • $\begingroup$ @holistic It sounds like you might want EdgeForm rather than a separate frame. I'll add an example of that in a moment. $\endgroup$
    – Mr.Wizard
    Jul 31, 2017 at 13:23
  • $\begingroup$ Thanks so much for those many examples, could learn a lot :). Yes, EdgeForm works very well, did not know about this yet! $\endgroup$
    – holistic
    Jul 31, 2017 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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