1
$\begingroup$

When I used Legended function, I got extra unwanted legend marker.

MSW:

ListPlot[Legended[Table[{RandomReal[], RandomReal[]}, {i, 10}], 
  Placed[LineLegend[{Directive[Thick]}, {"legend"}], {Right, 
  Top}]], Joined -> True]

and got the following result:

enter image description here

Clearly there's an extra blue line. How do I remove it?

$\endgroup$

closed as off-topic by Bob Hanlon, Carl Lange, m_goldberg, MarcoB, bbgodfrey Apr 25 at 15:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Bob Hanlon, Carl Lange, m_goldberg, MarcoB, bbgodfrey
If this question can be reworded to fit the rules in the help center, please edit the question.

1
$\begingroup$

When using Legended, the ListPlot is the argument.

SeedRandom[6];

Legended[
 ListPlot[
  Table[RandomReal[1, 2], {i, 10}],
  Joined -> True],
 Placed[
  LineLegend[
   {Directive[ColorData[97][1], Thick]}, {"legend"}],
  {Right, Top}]]

enter image description here

Alternatively, use the PlotLegends option to ListPlot

SeedRandom[6];

ListPlot[
 Table[RandomReal[1, 2], {i, 10}]
 PlotStyle->Thick,
 Joined -> True,
 PlotLegends -> Placed[{"legend"}, {Right, Top}]]
$\endgroup$
  • $\begingroup$ Thanks, I didn't realize I can wrap the whole plot as well. This solves my actual problem. But I still can't figure out why when the argument is the data, extra marker appears. Do you know why? $\endgroup$ – Turgon Apr 24 at 19:42
  • $\begingroup$ @Turgon - Because you included two different legends: 1) you Legended the argument to ListPlot, and 2) you added a LineLegend. Note that the first inherited the color of the default PlotStyle and the second has the style specified in the LineLegend. $\endgroup$ – Bob Hanlon Apr 24 at 20:02
3
$\begingroup$

The syntax you are using seems a bit odd. From the docs you can wrap your data in Legended[data, label], but in that case whatever is in label will be used to label data in the plot. Normally you would put something like "dataset 1" there, instead of a complete and placed legend.

Perhaps one of these will work:

ListPlot[RandomReal[1, {4, 2}],
    Joined -> True,
    PlotLegends -> Placed[LineLegend[{Directive[Thick]}, {"legend"}], {Right, Top}]
]

enter image description here

Or here, apply the style to the plot, and then the line legend will match the plot

ListPlot[RandomReal[1, {4, 2}],
    Joined -> True,
    PlotStyle -> Directive[Thick],
    PlotLegends -> Placed[LineLegend[{"legend"}], {Right, Top}]
]

enter image description here

This produces the same output, but maybe with syntax more to your liking

ListPlot[Legended[RandomReal[1, {4, 2}],"legend"],
    Joined -> True,
    PlotStyle -> Directive[Thick],
    PlotLegends -> Placed[Automatic, {Right, Top}]
]
$\endgroup$
  • $\begingroup$ According to the document one can put Placed[...] in the label section (the neat example), and that's necessary to style the legend. I know I can use PlotLegends for a single plot but I'm still curious why the example works and my code fails. $\endgroup$ – Turgon Apr 24 at 19:40
  • $\begingroup$ Your code doesn't fail, it does exactly what you are asking it to do. In your code, replace LineLegend[....] with "legend" and see what the result is, and it should make sense why your code behaves like it does. $\endgroup$ – Jason B. Apr 25 at 13:28

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