11
$\begingroup$

Using PlotLegends with a PlotStyle containing (for instance) PointSize[0.02] creates a legend that does not match the plot.

ListPlot[Table[RandomReal[NormalDistribution[], {20, 2}], {2}],
 PlotLegends -> {"a", "b"}, PlotStyle -> PointSize[0.02]]

enter image description here

How can I get a sensible legend when using a number with PointSize? Solutions involving named sizes (like PointSize[Medium]) are inadmissible unless they somehow create points that have a constant size relative to the plot size.

$\endgroup$

3 Answers 3

14
$\begingroup$

This problem often occurs in plotting and graphics because of the use of Foo instead of AbsoluteFoo for the directives. The former uses sizes relative to the plot size, whereas the latter, as the name suggests, uses absolute sizes.

In the built-in plot legends in version 9, the plot and the legend are actually two separate objects which are just combined, hence PointSize uses the figure size of each of those to compute what 0.02 means. All you need to do is to change it to AbsolutePointSize to keep it consistent between the two. Of course, the argument will also change and AbsolutePointSize[7] looks close enough to what you were gunning for.

$\endgroup$
1
  • 1
    $\begingroup$ Thanks for your answer. I'm disappointed because this kind of trial end error is what I had hoped would be eliminated by the new legends capabilities. I'd say it's not mature yet. $\endgroup$ Commented Jan 10, 2013 at 18:05
7
$\begingroup$

In version 10 the PointSize of the legend will automatically match the PointSize of the Plot:

ListPlot[Table[RandomReal[NormalDistribution[], {20, 2}], {2}], 
  PlotLegends -> {"a", "b"}, PlotStyle -> PointSize[0.02]]

MmaVersion10


Edit: The answer by @eldo made me realize, that this is only true up to a PointSize that is equal to the default LegendMarkerSize. To get bigger matching PointSizes:

ListPlot[Table[RandomReal[NormalDistribution[], {20, 2}], {2}], 
 PlotStyle -> AbsolutePointSize[15], 
 PlotLegends -> PointLegend[{"a", "b"}, LegendMarkerSize -> 15]]

evenBiggerPoints

For further testing you can use:

Manipulate[
 ListPlot[Table[RandomReal[NormalDistribution[], {20, 2}], {2}], 
  PlotStyle -> AbsolutePointSize[ps], 
  PlotLegends -> PointLegend[{"a", "b"}, LegendMarkerSize -> ps]],
 {ps, 1, 50}]
$\endgroup$
3
  • $\begingroup$ Excellent @Karsten ! I was already working on complicated things :) $\endgroup$
    – eldo
    Commented Aug 13, 2014 at 14:59
  • $\begingroup$ @eldo Agree. They improved the PlotLegends, but some things still don't work as automated and intuitive as they should and one ends up doing way to complicated stuff. $\endgroup$
    – Karsten7
    Commented Aug 13, 2014 at 15:11
  • $\begingroup$ BTW, Chapeau for collecting 1.651 pennies within 33 days of membership. Hope your website will pop up soon. $\endgroup$
    – eldo
    Commented Aug 13, 2014 at 18:54
2
$\begingroup$

My V10 output doesn't agree with Karsten's answer, f.e.:

ListPlot[Table[RandomReal[NormalDistribution[], {20, 2}], {2}],
 PlotLegends -> {"a", "b"},
 PlotStyle -> PointSize[0.05]]

enter image description here

I tried to fix this by building my own PointLegend:

PointLegend[{
  Directive[Blue, AbsolutePointSize[20]],
  Directive[Orange, AbsolutePointSize[20]]},
 {"a", "b"}]

enter image description here

Unfortunately, above a certain AbsolutePointSize the points don't grow anymore but mutate to rectangles.

$\endgroup$

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.