7
$\begingroup$

Suppose I want a line legend for a list plot:

dat = RandomReal[{.8, 1.2}, 40]*Range@40;

ListPlot[Transpose@{Range@40, dat}, 
PlotLegends -> 
Placed[LineLegend[{Style["I want a line legend!!!", 
  18]}], {.4, .8}]]

However, the output doesn't show a line legend but something that looks like a point legend:

enter image description here

I use Mathematica 10.0.2 on Windows 7. Is this a bug?

$\endgroup$
  • 1
    $\begingroup$ Mma 10 -- there are a few sub-versions, please, add the necessary version info. $\endgroup$ – Sektor Jul 31 '15 at 13:45
  • $\begingroup$ To check version, use Help -> About in the GUI or use both $Version and $ReleaseNumber. Unfortunately $Version and $VersionNumber will not contain the z part of an x.y.z form version number. $\endgroup$ – Szabolcs Jul 31 '15 at 14:23
  • $\begingroup$ @Szabolcs how did you know that my $ReleaseNumber gives 2? By GUI->About It's 10.0.2 indeed! $\endgroup$ – Harry Jul 31 '15 at 14:30
  • $\begingroup$ @Harry I have 10.0.2 installed and the date of December 4, 2014 (in $Version) matches with yours. If yours showed an earlier date, I couldn't tell if it was 10.0.0 or 10.0.1 as I don't have those versions any more. $\endgroup$ – Szabolcs Jul 31 '15 at 14:34
13
$\begingroup$

This is not a bug. I think that this is not a bug, see addendum.

Based on my (limited!) experience, I believe that LineLegend and PointLegend are in fact the very same thing with differing default options. LineLegend has Joined -> True while PointLegend has Joined -> False by default, but otherwise they are identical.

The syntax you used, i.e. LineLegend[{"label1", "label2", ...}] causes the legend to inherit the style from the plot, which means Joined -> False. You can fix this by specifying the option manually.

LineLegend[{Style["I want a line legend!!!", 18]}, Joined -> True]

If you use PointLegend instead, it will do the same thing. The only important factor is the values of the Joined option.

If you use these legends outside of the PlotLegends option of some type of plot, they will not inherit any options and will behave as their name would suggest (and as determined by their default options).


Addendum

Harry points out that in version 9 these legends do not inherit the Joined option from the parent plot, thus LineLegend always gives a line and PointLegend always gives a point.

Should we consider this behaviour change a breakage in v10? I don't think so because the v9 behaviour also causes problems. In v9, ListPlot[Transpose@{Range@40, dat}, PlotLegends -> {"label"}, Joined -> True] uses a point legend, even though the plot has a line. This seems to have been fixed in v10 by causing the legend to inherit the Joined option from the parent.

$\endgroup$
  • $\begingroup$ seems it's a new feature of version10(version 9 behaves differently). It's quite reasonable after all. $\endgroup$ – Harry Jul 31 '15 at 14:25
  • $\begingroup$ @Harry You're right, see update. $\endgroup$ – Szabolcs Jul 31 '15 at 14:33
2
$\begingroup$
$Version

"10.2.0 for Mac OS X x86 (64-bit) (July 7, 2015)"

dat = Transpose@{Range@40, RandomReal[{.8, 1.2}, 40]*Range@40};

Legended[
 ListPlot[dat],
 Placed[
  LineLegend[{Blue},
   {Style["I want a line legend!!!", 14]}],
  {.3, .8}]]

enter image description here

$\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.