Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Working some charts on V10, I discovered that PlotLegends disappears if used together with ColorFunction inside ListPlot or Plot. Here is one example for Plot:

Plot[Sinc[x],{x,0,10},PlotLegends->{"Teste"},PlotStyle->Thick]

enter image description here

Plot[Sinc[x],{x,0,10}, PlotLegends->{"Teste"}, PlotStyle->Thick
     ,ColorFunction->(ColorData["AvocadoColors"][#2]&)
]

enter image description here

The same problem to ListPlot.

I'm on Mac using V10. Anyone else confirm this?

share|improve this question
    
same here. Also on OS X –  acl Jul 11 at 1:16
    
Does it work in 9? I wouldn't have thought so. Legend lines tend to be mono colour. ColorData["AvocadoColors"] precludes this. –  Mike Honeychurch Jul 11 at 1:22
1  
A legend is produced in 9 but of course it doesn't actually match the colours used in the plot. So I'd class that as a bug in 9 and this as a fix in 10. –  Mike Honeychurch Jul 11 at 1:24
    
You can use ColorFunction -> (Red &), and have the same problem. I just tested in the company machine (still V9) and It works. It's a V10 bug. –  Murta Jul 11 at 1:46
    
So you are expecting the legend line to be a colour gradient? What does (ColorData["AvocadoColors"][#2]&) give you on your V9? Mine gives a legend with a different colour line –  Mike Honeychurch Jul 11 at 1:59

4 Answers 4

up vote 2 down vote accepted

One can use gradient color schemes for LineLegend

Plot[{Sinc[x], Sin[x], Cos[x]}, {x, 0, 10},
 PlotStyle -> Thickness[0.01], 
 ColorFunction -> Function[{x, y}, ColorData["NeonColors"][x]],
 PlotLegends -> LineLegend["NeonColors", {"Teste", "Tuste", "Taste"}],
 PlotTheme -> "Scientific"]

enter image description here

?? LineLegend

will reveal some hints:

enter image description here

share|improve this answer

A work-around:

plt = Plot[Sinc[x], {x, 0, 10}, PlotStyle -> Thick,
           ColorFunction -> (ColorData["AvocadoColors"][#2] &), ImageSize -> 400];
legend = Row[{Graphics[plt[[1]], AspectRatio -> 1, ImageSize -> 30], "Teste"}];
Legended[plt, legend]

enter image description here

In Version 10, one can use PlotTheme->"Sparkline" to create the thumbnail legend:

legend2=Plot[Sinc[x], {x, 0, 10}, PlotStyle -> Thick, 
            PlotLegends->"Teste",
            ColorFunction -> (ColorData["AvocadoColors"][#2] &),
            PlotTheme->"Sparkline",AspectRatio->1, ImageSize->30] 

... and another:

Plot[Sinc[x], {x, 0, 10}, PlotStyle -> Thick, ImageSize -> 400,
  ColorFunction -> (ColorData["AvocadoColors"][#2] &),
  PlotLegends -> Row[{Graphics[{Thick, Line[{{0, 0}, {1, 0}},
              VertexColors -> {ColorData["AvocadoColors"][0],
                               ColorData["AvocadoColors"][2]}]}], "Teste"}, Spacer[5]]]

enter image description here

share|improve this answer
1  
+1: mini plot as legend marker nice –  ubpdqn Jul 11 at 6:06

I appreciate that this is a question with philosophical and technical dimensions about automation. Like kguler a work around:

Plot[Sinc[x], {x, 0, 10}, PlotStyle -> Thick, 
 ColorFunction -> (ColorData["AvocadoColors"][#2] &), 
 ImageSize -> 400, 
 PlotLegends -> 
  BarLegend["AvocadoColors", LegendMarkers -> None, 
   LegendLabel -> Style["Teste", Black], LabelStyle -> White, 
   LegendMarkerSize -> {3, 150}]]

enter image description here

I made the legend vertical just to match the scaling by 'y' value.

Or:

Plot[Sinc[x], {x, 0, 10}, PlotStyle -> Thick, 
 ColorFunction -> (ColorData["AvocadoColors"][#2] &), 
 ImageSize -> 400, 
 PlotLegends -> 
  BarLegend["AvocadoColors", LegendLayout -> "Row", 
   LegendMarkers -> None, LegendLabel -> Style["Teste", Black], 
   LabelStyle -> White, LegendMarkerSize -> {150, 3}]]

enter image description here

share|improve this answer

if you want same legend as in plot1, maybe also some round way like this:

p1 = First@Last@Plot[Sinc[x];, {x, 0, 10}, PlotLegends -> {"Teste"}];
Row[{Plot[Sinc[x], {x, 0, 10}, PlotLegends -> {"Teste"}, 
   PlotStyle -> Thick, 
   ColorFunction -> (ColorData["AvocadoColors"][#2] &), 
   ImageSize -> 300], p1}]

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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