2
$\begingroup$

I'm trying to plot this simple code

StreamDensityPlot[{x y Sin[4 x], x y Cos[4 y]}, {x, -L, L}, {y,-L,L}, PlotLegends -> Automatic, StreamStyle -> Black,  ColorFunction -> (ColorData["VisibleSpectrum"][Rescale[#5, {0, 1.2}, {380, 750}]] &),ColorFunctionScaling -> False,PlotLegends -> Placed[BarLegend[{Automatic, {0, 1.2}}], Above]]

However, even though I used the Colorfunctionscaling as being False, and asking later for the plotlegends, the legend bar seems not to appear. Any thoughts? Thanks!

enter image description here

$\endgroup$
4
  • 1
    $\begingroup$ use PlotLegends -> Placed[BarLegend[{"VisibleSpectrum", {380, 750}}], Above]? (and remove the first PlotLegends -> Automatic in your code). $\endgroup$
    – kglr
    Nov 2, 2020 at 19:58
  • $\begingroup$ Thanks @kglr. Now I see the bars! However, I'd like to access the value of the function based on its color. When I set the bar like you did, it spans from 380 to 750 instead of from 0 to 1.2 $\endgroup$
    – sined
    Nov 2, 2020 at 20:13
  • 1
    $\begingroup$ how about PlotLegends ->Placed[BarLegend[{(ColorData["VisibleSpectrum"][Rescale[#, {0, 1.2},{380,750}]]&), {0, 1.2}}], Above]? $\endgroup$
    – kglr
    Nov 2, 2020 at 20:19
  • $\begingroup$ Yep. That definitely worked! Thanks a lot! $\endgroup$
    – sined
    Nov 2, 2020 at 20:23

1 Answer 1

2
$\begingroup$

Remove PlotLegends -> Automatic and use

PlotLegends -> Placed[
  BarLegend[{(ColorData["VisibleSpectrum"][Rescale[#, {0, 1.2},{380,750}]]&), {0, 1.2}}],
  Above]

to get

enter image description here

$\endgroup$

Your Answer

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

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