# Wrong BarLegend with VisibleSpectrum

Plot3D[Exp[-x^2 - y^2], {x, -2, 2}, {y, -2, 2},   ColorFunction -> (ColorData["VisibleSpectrum"][
Rescale[#3, {0, 1}, {380, 750}]] &), PlotLegends -> Automatic]


There is no legend in this picture.

Plot3D[Exp[-x^2 - y^2], {x, -2, 2}, {y, -2, 2},
ColorFunction -> (ColorData["VisibleSpectrum"][Rescale[#3, {0, 1}, {380, 750}]] &),
PlotLegends -> BarLegend[{"VisibleSpectrum", {0, 1}}]]


BarLegend["VisibleSpectrum"]


The color of lengend is wrong.

Mathematica 10.0.1. Is this a bug?

• I think the correct usage of the second argument should be BarLegend[{"VisibleSpectrum", {380, 750}}] but no, that doesn't work either... Commented Nov 24, 2014 at 5:19
• related: 38895. @Pickett almost :) see my answer.
– Kuba
Commented Nov 24, 2014 at 7:22
• @Kuba wow, I only tried BarLegend on its own. You can't even set the scaling then as far I as I know. I count this as a bug.. Commented Nov 24, 2014 at 12:09

f@x_ := ColorData["VisibleSpectrum"][Rescale[x, {0, 1}, {380, 750}]];
Plot3D[
Exp[-x^2 - y^2], {x, -2, 2}, {y, -2, 2},
ColorFunction -> f, PlotLegends -> BarLegend[{f@# &, {0, 1}}]]


• I'm trying to perform your suggestion in a similar problem. However, here I'm using StreamDensityPlot instead of Plot3D. Therefore, my bar refers to the absolute value of the vector, i.e., ColorFunction -> (ColorData["VisibleSpectrum"][ Rescale[#5, {0, 1.2}, {380, 750}]] &). How could I adjust your code accordingly? Commented Nov 2, 2020 at 20:15