8
$\begingroup$

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?

$\endgroup$
7
  • $\begingroup$ same here. Also on OS X $\endgroup$
    – acl
    Jul 11 '14 at 1:16
  • $\begingroup$ Does it work in 9? I wouldn't have thought so. Legend lines tend to be mono colour. ColorData["AvocadoColors"] precludes this. $\endgroup$ Jul 11 '14 at 1:22
  • 1
    $\begingroup$ 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. $\endgroup$ Jul 11 '14 at 1:24
  • $\begingroup$ 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. $\endgroup$
    – Murta
    Jul 11 '14 at 1:46
  • $\begingroup$ 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 $\endgroup$ Jul 11 '14 at 1:59
2
$\begingroup$

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

$\endgroup$
9
$\begingroup$

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

$\endgroup$
0
6
$\begingroup$

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

$\endgroup$
1
$\begingroup$

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

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