# BarLegend using Function that uses Blend

Bug introduced in 10.0 and fixed in 10.1

I'm sorry if this is a duplicate, but I don't even know how to look for it.

I had this line of code that worked on a Notebook I wrote on Mathematica 9.0:

BarLegend[{colorf[#] &, {0, 30}}]


colorf is a Blend going from 0 to 30 (40 actually, but I don't think it really matters).

I just installed Mathematica 10.0, and it no longer works: the bar is all red (i.e. colorf[0]). Why is this happening?

edit: colorf is defined as follows

colorf=Blend[{{0, Red}, {20, Yellow}, {40, Green}}, Round[#, 0.1]] &;

• colorf = Blend[{Red, Blue}, #] &; BarLegend[{colorf, {0, 30}}] works fine in V. 10.0.2. -- maybe it is the definition of colorf and using colof[#] &?
– gwr
Dec 11, 2014 at 21:46
• Could you provide all necessary code? @gwr's code works for me as well, so you should provide more information in order to reproduce the problem. Dec 11, 2014 at 21:52
• Thanks for asking. I just put it into the question Dec 11, 2014 at 23:30
• To me this looks like a bug in the scaling of BarLegends with continuous gradients introduced in version 10 (I'm using v10.0.2). Dec 12, 2014 at 2:30

To me it looks like there is a bug in the Implementation of BarLegend. When the the number of contours increases there is not only a switch from discrete contours to a continuous gradient (this behavior is documented), but also a change in the scaling (that's the bug).

colorf = Blend[{{0, Red}, {20, Yellow}, {40, Green}}, Round[#, 0.1]] &;
BarLegend[{colorf[#] &, {0, 30}}, #] & /@ Range[15]


One can use Rescale to fix this:

colorf2 = Blend[{{0, Red}, {20, Yellow}, {40, Green}},
Round[Rescale[#, {0, 1}, {0, 40}], 0.1]] &;
BarLegend[{colorf2[#] &, {0, 30}}]


But now one has to be aware that BarLegend with discrete contours is broken:

BarLegend[{colorf2[#] &, {0, 30}}, #] & /@ Range[15]


• Wow, impressive. Thanks. However, the problem now is that yellow is 15, when it should be 20... Shall I just divide the parameter by 40? Dec 12, 2014 at 10:57
• Finally I solved the 15->yellow problem by doing BarLegend[{colorf[#*30] &, {0, 30}}]. Not the cleanest of solutions, but does the trick pretty well and doesn't need to modify the color function. I'm chosing this answer for completeness. Thanks to both! Dec 12, 2014 at 11:12
• @Rafael You can also use BarLegend[{colorf2[# 30/40] &, {0, 30}}]. Dec 12, 2014 at 11:24

Blend was modified in version 10. It appears that Blend is now scaled to {0, 1}.

colorf = Blend[{{0, Red}, {0.5, Yellow}, {1, Green}}, Round[#, 0.01]] &;

BarLegend[{colorf, {0, 30}}]


• It seems to be no issue of Blend, but a bug in the scaling of BarLegend. See me answer and try Graphics[Table[{colorf[x], Disk[{x, 0}]}, {x, 1, 40}]]. Dec 12, 2014 at 2:34
• Interesting, both answers have good points but there seems to be indeed a bug as BarLegend[{colorf, {0, 30}},n] will give false answers for small n as shown in @Karsten 7's answer. BTW why is the Round[#, 0.001] needed?
– gwr
Dec 12, 2014 at 10:05
• @gwr In my opinion the Round is not needed, but it's in the code provided by the OP and I left it in the code to show that it's not the reason for the problem. Dec 12, 2014 at 10:23
• The purpose of round, as far as I remember, had to do with the main plot (to which this was but the legend). I think it was creating too many colors and thus leaking memory or something (there were thousands of points, nearly everyone with its own color) Dec 12, 2014 at 11:05