4
$\begingroup$

I'm trying to get my PlotMarkers to appear in the Legend as they do on the plot. I've set up a function named square[], that makes a square with black edges. When apply this function in 'LegendMarkers' the edges appear gray in the Legend. Its really annoying? Any ideas on how to fix this? image and script are below.

enter image description here

square[in_, out_: Black, size_: 12] := 
 Graphics[{in, EdgeForm[{AbsoluteThickness[1.5], out}], Rectangle[]}, 
  PlotRangePadding -> 0, ImageSize -> size]

data = {{2, 4}, {3, 9}, {4, 16}, {5, 25}, {6, 36}}

legend = PointLegend[{Style["test", 18]} , 
   LegendMarkers -> square[Red]];

ListPlot[data,
 PlotMarkers -> {square[Red]},
 PlotLegends -> Placed[legend, {.2, .7}]]
$\endgroup$
1
$\begingroup$

Update: Grayish edges on the legend markers are the result of default setting for marker style:

Legending`LegendDump`$DefaultMarkerStyle

EdgeForm[Directive[Opacity[0.3], GrayLevel[0]]]

You can reset the default edge opacity to Opacity[1]:

Legending`LegendDump`$DefaultMarkerStyle = EdgeForm[Opacity[1]];

PointLegend[{Red}, {Style["test", 18]}, LegendMarkers -> square[Red], 
 LegendMarkerSize -> 20]

enter image description here

Original answer:

Change the default value of the second argument to set the Opacity of edges to 1:

square2[in_, out_: Opacity[1, Black], size_: 12] := 
 Graphics[{in, EdgeForm[{AbsoluteThickness[1.5], out}], Rectangle[]}, 
  PlotRangePadding -> 0, ImageSize -> size]

Compare sqare vs. square2:

PointLegend[{Red}, {Style["test", 18]}, LegendMarkers -> square[Red], 
 LegendMarkerSize -> 20]

Mathematica graphics

PointLegend[{Red}, {Style["test", 18]}, LegendMarkers -> square2[Red],
  LegendMarkerSize -> 20]

Mathematica graphics

ListPlot[data, PlotMarkers -> square2[Red], 
 PlotLegends ->  Placed[PointLegend[{Style["test", 18]}, 
    LegendMarkers -> square2[Red]], {.2, .7}]]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Why is this tweak mecessary for the legend but not the plot? $\endgroup$ – Myridium Oct 17 '16 at 14:29
0
$\begingroup$

Interesting observation. You appear to be using code from my answer to Change the inner color of markers and on inspection I saw that the legends there were affected as well. I have amended my code to specify a solid Opacity.

The question remains however why this happens. Looking at the Box out gives us a clue:

PointLegend[{1}, {"test"}, LegendMarkers -> square[Red]] // ToBoxes

In that large output we have:

GraphicsBox[{RGBColor[1, 0, 0], EdgeForm[{AbsoluteThickness[2], GrayLevel[0]}], 
  RectangleBox[{0, 0}]}, {DefaultBaseStyle -> {"Graphics", {AbsolutePointSize[6]}, 
    Directive[EdgeForm[Directive[Opacity[0.3`], GrayLevel[0]]], PointSize[0.5`], 
     AbsoluteThickness[1.6`]]}, ImageSize -> 13}]

So somewhere a DefaultBaseStyle that includes an EdgeForm with Opacity[0.3`] is inserted.

This seems to be a stylistic choice for e.g.

SwatchLegend[{Red, Green, Blue}, Automatic] ~Magnify~ 3

enter image description here

I don't know if you are interested in tracking down the ultimate source of this directive or if that answers your question. kglr gave the direct solution, and if you use marker from my referenced answer you will find it now works too.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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