# Why legendmarkers shifted a few pixels after opacity is specified

In below simple graph, if an opacity is specified in the legendmarkers, the plot marker shifted a little bit, can someone please shed some lights?

Plot[{Sin[x], Cos[x]}, {x, 0, Pi},
PlotLegends -> {Placed[PointLegend[{Directive[Red, Opacity[0.8]], Blue},
TextCell[#, TextAlignment -> Left] & /@ {"ABC", "XYZ"},
LegendMarkers -> {{\[FilledSquare], 22}, {\[FilledSquare], 22}}], {0.5, 0.5}]}]


## 2 Answers

I can confirm the shift occurs even without the special default styling in your plot (version 9).

Your issue stems directly from the use of a special font marker \[FilledSquare] in a PointLegend instead of just using a SwatchLegend. The following does not have the shifting problem.

Plot[{Sin[x], Cos[x]}, {x, 0, Pi},
PlotLegends -> Placed[SwatchLegend[{Directive[Opacity[0.8], Red], Blue}, {"ABC", "XYZ"}],
{0.5, 0.3}]]


If you really want to use PointLegend and a square for this, you can use a graphics directive rather than a special character, as shown.

Plot[{Sin[x], Cos[x]}, {x, 0, Pi},
PlotLegends -> {Placed[
PointLegend[{Directive[Red, Opacity[0.8]], Blue},
TextCell[#, TextAlignment -> Left] & /@ {"ABC", "XYZ"},
LegendMarkerSize -> 13,
LegendMarkers -> Graphics[{Rectangle[{0, 0}]}]], {0.5, 0.5}]}]


Neither workaround above has the shifting problem.

In any case, I would caution against the use of special characters like \[FilledSquare] as graphics elements. They require the use of the special Mathematica fonts which are not available on all computers and fail badly when exporting as EMF.

As an aside, I'm trying to understand why you need Opacity directives at all. These will not work in EMF export and (because I know what application you are working on ;-) ) are not required by the business.

As a further aside, the use of TextCell stuff isn't necessary to get the style in the legend labels. See, for example:

Plot[{Sin[x], Cos[x]}, {x, 0, Pi},
BaseStyle -> {FontFamily -> "Arial", FontSize -> 20},
PlotLegends ->
Placed[SwatchLegend[{Directive[Opacity[0.8], Red], Blue}, {"ABC",
"XYZ"}, LegendMarkerSize -> 18,
LabelStyle ->
Directive[FontFamily -> "Arial", FontSize -> 20]], {0.5, 0.25}]]


Edit in response to further discussion in comments

Part of the issue here is that the Legend commands by default add a thin border to the legend marker. If this is not wanted, one could either specify EdgeForm[] in the style of the marker, like the first example below, or one could set the BaseStyle to be EdgeForm[] as in the second example. As described in an earlier question and self-answer of mine, BaseStyle does actually work in the various *Legend commands even though the syntax highlighter colors it in red.

Plot[{Sin[x], Cos[x]}, {x, 0, Pi},
BaseStyle -> {FontFamily -> "Arial", FontSize -> 20},
PlotLegends -> Placed[SwatchLegend[{Directive[EdgeForm[], Red],
Directive[EdgeForm[], Blue]}, {"ABC", "XYZ"},
LegendMarkerSize -> 18,
LabelStyle -> Directive[EdgeForm[], FontFamily -> "Arial",
FontSize -> 20]], {0.5, 0.25}]]

Plot[{Sin[x], Cos[x]}, {x, 0, Pi},
BaseStyle -> {FontFamily -> "Arial", FontSize -> 20},
PlotLegends ->  Placed[SwatchLegend[{Red, Blue}, {"ABC", "XYZ"},
BaseStyle -> EdgeForm[], LegendMarkerSize -> 18,
LabelStyle -> Directive[EdgeForm[], FontFamily -> "Arial",
FontSize -> 20]], {0.5, 0.25}]]

• thanks for the suggestions, notes taken. Back to the original code, I used FullForm and discover that when Opacity is applied, PointLegends add edges around the drawing object, by applying Directives of EdgeForm[List[Opacity[0.3],GrayLevel[0]]] (1 level lower than the specified opacity). And I am just trying to resolved one of the old tickets assigned to me, admit opacity on legends is not necessary. – Leonx Sep 19 '14 at 3:21
• @Leonx You don't want the borders anyway, trust me on that one. You can get rid of them by specifying Directive[EdgeForm[], Red]. I'll update my answer. And without wanting to seem pushy, please note that I now consult to the team in exchange for upvotes and answer-accepts ;-) – Verbeia Sep 19 '14 at 3:27

Verbeia already gave you more robust alternative methods, but in direct answer to the question I believe this is due to the use of a font glyph for \[FilledSquare] and the interaction of these two issues:

In summary:

1. Font glyphs are subject to alignment errors due to one of the anti-aliasing methods used by Mathematica

2. Using Opacity changes the anti-aliasing method and therefore the alignment.

The simplest solution for this particular problem is probably to set Opacity[.999]` for all other markers, which should cause all of them to use the same AA method and therefore align better.

• thanks for explaining in details. – Leonx Sep 21 '14 at 23:41