2
$\begingroup$

I want to create a custom BarLegend with continuous colors:

Block[{minmax,ticks, cf},
    ticks = Function[{min, max},({#,StringForm["`` dBa",#]} )&  /@ Range[min,max]];
    minmax = {70,90};
    cf = ColorData["LightTemperatureMap"][Rescale[#, minmax]] &;
    BarLegend[{cf, minmax}, "Ticks" -> ticks @@minmax]
]

Ticks are properly displayed. But the BarLegend is drawn using a solid color. What did I do wrong?

Bar legend rendered with solid colors instead of a continuous spectrum

$\endgroup$
3
$\begingroup$

Moving the minmax constant out of the Block construct works:

With[{
        minmax = {70,90}
    },Block[{ticks, cf},
        ticks = Function[{min, max}, ({#,StringForm["`` dBa",#]} )&  /@ Range[min,max]];
        cf = ColorData["LightTemperatureMap"][Rescale[#, minmax]] &;
        BarLegend[{cf, minmax}, "Ticks" -> ticks @@minmax]

    ]
]

enter image description here

I can't explain why, though.

|improve this answer|||||
$\endgroup$
  • 2
    $\begingroup$ If you look at the InputForm of your BarLegend you can see what's going wrong: The minmax inside cf is stored in its unevaluated form inside the legend expression. When the expression is finally displayed, it fails, as minmax is no longer set. (You can think of BarLegend and similar legend expressions as analogues to Graphics expressions - they don't evaluate on their own, they are simply typeset in a non-standard way, resulting in graphical output instead of the raw expressions.) $\endgroup$ – Lukas Lang Feb 23 at 14:12
  • $\begingroup$ Thanks, Lukas. Actually, this is exactly what I did and how I had the idea to wrap the Block inside a With! Unfortunately, I didn't quite understand why minmax was not bound to the actual value. I didn't use Mathematica for a few weeks, and I think I've forgotten (again!) the difference of semantic between Block and With. :/ $\endgroup$ – Sylvain Leroux Feb 23 at 14:26

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.