4
$\begingroup$

The following code gives legend only for elements with unique color

ListPlot[RandomReal[1, {4, 10}],
 Joined -> True,
 PlotRange -> All,
 PlotStyle -> ColorData[3, "ColorList"][[;; 2]],
 PlotLegends -> LineLegend[Automatic, Range[4]]]

or

ListPlot[RandomReal[1, {4, 10}],
 Joined -> True,
 PlotRange -> All,
 PlotStyle -> ColorData[3, "ColorList"][[;; 2]],
 PlotLegends -> 
  LineLegend[ColorData[3, "ColorList"][[;; 2]], Range[4]]]

enter image description here

This simpler code provides expected result

ListPlot[RandomReal[1, {4, 10}],
 Joined -> True,
 PlotRange -> All,
 PlotStyle -> ColorData[3, "ColorList"][[;; 2]],
 PlotLegends -> Range[4]]

enter image description here

I only came to this bug in a rare usage case. Usually one would not label datasets with a same color differently, but perhaps the bug effects some reasonable usage as well.

Mathematica V10.2, Windows 8 64 bit

Edit It actually has nothing to do with the Plot itself. Already this gives what I would call an unexpected behavior if you compare it to how Plot handles lack of unique colors.

LineLegend[ColorData[3, "ColorList"][[;; 2]], Range[4]]

enter image description here

$\endgroup$
14
  • $\begingroup$ According to the docs (See under Scope -> Data), LineLegend remains unevaluated when first its parameter is Automatic. So, it is not a bug. $\endgroup$
    – SquareOne
    Jul 24, 2016 at 19:31
  • $\begingroup$ @SquareOne The section in the documentation refers to call of LineLegend outside Plot. In case it is wrapped in Plot it does get evaluated as I have illustrated. I also added two more examples (at the moment second and fourth image), which illustrate behavior also contradicting your interpretation. $\endgroup$
    – Johu
    Jul 24, 2016 at 21:39
  • 1
    $\begingroup$ I thought you meant a bug report for Wolfram Support. Did you send one there as well? I removed the bugs tag for the time being. Please check mathematica.stackexchange.com/tags/bugs/info for the general procedure for this special tag. $\endgroup$
    – Karsten7
    Jul 24, 2016 at 21:53
  • 2
    $\begingroup$ @Johu You are right for Automatic. But concerning your edit, actually in the same Scope/Data section of the docs, there is an example saying that "LineLegend uses the shorter list of styles and labels" with this example: LineLegend[{Red, Green, Blue}, {"label1", "label2", "label3", "label4"}] showing that LineLegenddoes not know to cycle colors. $\endgroup$
    – SquareOne
    Jul 24, 2016 at 22:40
  • 2
    $\begingroup$ @Mr.Wizard The last example in the OP here: ListPlot[RandomReal[1, {4, 10}],Joined -> True,PlotRange -> All, PlotStyle -> ColorData[3, "ColorList"][[;; 2]],PlotLegends -> Range[4]] shows a correct behaviour of PlotLegends: it cycles the two colors in the PlotStyle, so it produces more labels than styles. I think the problem here is as I said that LineLegend prevents PlotStyle from cycling colors. Maybe that means (concerning the question 66055) that PlotLegends->"Expressions" or PlotLegends->Automatic uses LineLegend underneath ... ? $\endgroup$
    – SquareOne
    Jul 24, 2016 at 23:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.