9
$\begingroup$

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]] &;
$\endgroup$
4
  • $\begingroup$ 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[#] &? $\endgroup$
    – gwr
    Dec 11, 2014 at 21:46
  • 1
    $\begingroup$ 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. $\endgroup$ Dec 11, 2014 at 21:52
  • $\begingroup$ Thanks for asking. I just put it into the question $\endgroup$
    – Rafael
    Dec 11, 2014 at 23:30
  • 1
    $\begingroup$ 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). $\endgroup$
    – Karsten7
    Dec 12, 2014 at 2:30

2 Answers 2

11
$\begingroup$

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]

enter image description here

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}}]

enter image description here

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

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

enter image description here

$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$
    – Rafael
    Dec 12, 2014 at 10:57
  • 2
    $\begingroup$ 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! $\endgroup$
    – Rafael
    Dec 12, 2014 at 11:12
  • $\begingroup$ @Rafael You can also use BarLegend[{colorf2[# 30/40] &, {0, 30}}]. $\endgroup$
    – Karsten7
    Dec 12, 2014 at 11:24
5
$\begingroup$

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}}]

enter image description here

$\endgroup$
4
  • 2
    $\begingroup$ 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}]]. $\endgroup$
    – Karsten7
    Dec 12, 2014 at 2:34
  • 2
    $\begingroup$ 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? $\endgroup$
    – gwr
    Dec 12, 2014 at 10:05
  • 1
    $\begingroup$ @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. $\endgroup$
    – Karsten7
    Dec 12, 2014 at 10:23
  • 1
    $\begingroup$ 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) $\endgroup$
    – Rafael
    Dec 12, 2014 at 11:05

Your Answer

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

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