5
$\begingroup$

Context

Let me define two colour tables: (which come from brewer and xmedcom respectively)

   GoldColor = Blend[{{0, Black}, 
   {1/9, RGBColor[32/97, 1/62, 0]}, 
   {2/5, RGBColor[44/59, 23/78, 1/32]}, 
   {3/5, RGBColor[84/85, 1/2, 4/51]}, 
   {2/3, RGBColor[84/85, 53/87, 1/10]}, 
   {4/5, RGBColor[84/85, 4/5, 11/32]}, 
   {9/10, RGBColor[84/85, 68/75, 46/75]}, 
   {1, White}}, #1] & 

and

GalColor = Blend[{
     {0, RGBColor[7/11, 0, 1/7]}, 
     {1/17, RGBColor[10/13, 1/9, 1/7]}, 
     {31/255, RGBColor[7/8, 1/4, 1/6]}, 
     {47/255, RGBColor[13/14, 5/13, 1/4]}, 
     {21/85,RGBColor[31/32, 5/9, 4/13]}, 
     {79/255,RGBColor[46/47, 7/10, 2/5]}, 
     {19/51, RGBColor[46/47, 5/6, 9/17]}, 
     {37/85, RGBColor[1, 10/11, 5/8]}, 
     {127/255, RGBColor[1, 1, 3/4]},
     {143/255, RGBColor[10/11, 22/23, 7/8]}, 
     {53/85, RGBColor[5/6, 11/12, 17/18]}, 
     {35/51, RGBColor[7/10, 6/7, 10/11]}, 
     {191/255, RGBColor[4/7, 10/13, 6/7]}, 
     {69/85, RGBColor[3/7, 21/32, 4/5]}, 
     {223/255, RGBColor[1/3, 8/15, 11/15]}, 
     {239/255,RGBColor[1/4, 5/13, 2/3]}, 
     {1, RGBColor[1/5, 3/13, 3/5]}}, #1] & 

which looks like this:

GraphicsRow[{ContourPlot[x y , {x, -1, 1}, {y, -1, 1}, 
   ColorFunction -> GalColor],
  ContourPlot[x y , {x, -1, 1}, {y, -1, 1}, 
   ColorFunction -> GoldColor]}]

Mathematica graphics

But if I use it on large data sets, the first one works fine

 dat = RandomReal[{0, 1}, {512, 512}];
 Graphics[Raster[dat, ColorFunction -> GoldColor]]

Mathematica graphics

whereas the second one fails with Raster

 Graphics[Raster[dat, ColorFunction -> GalColor]]

 (* void plot *)

while it works fine with MatrixPlot

Mathematica graphics

Note that the problem depends on the size of dat; e.g. it works fine with 256^2.

Question

How come Raster fails on this color table?

$\endgroup$
3
  • $\begingroup$ Both colour functions fail for me with Raster. A workaround is to inject the evaluated colour data into Function using With : GalColor = With[{x = big list}, Blend[x, #]&] $\endgroup$ Mar 16, 2013 at 17:02
  • $\begingroup$ Indeed it does. May be you should write it up as an answer? $\endgroup$
    – chris
    Mar 16, 2013 at 17:23
  • $\begingroup$ Intriguingly it is also much faster! @Mr.Wizard would this answer my other question? Why? $\endgroup$
    – chris
    Mar 16, 2013 at 17:26

2 Answers 2

5
$\begingroup$

Both colour functions fail for me with Raster.

I don't have an answer to why it happens, but I do have a workaround, which is to inject the evaluated colour data into Function using With:

GoldColor = With[{x = {{0, Black}, 
{1/9, RGBColor[32/97, 1/62, 0]}, 
{2/5, RGBColor[44/59, 23/78, 1/32]}, 
{3/5, RGBColor[84/85, 1/2, 4/51]}, 
{2/3, RGBColor[84/85, 53/87, 1/10]}, 
{4/5, RGBColor[84/85, 4/5, 11/32]}, 
{9/10, RGBColor[84/85, 68/75, 46/75]}, 
{1, White}}}, Blend[x, #]& ]

The only difference between the function defined this way, and the original, is that the evaluation converts the fractions to Rational, e.g. Times[7, Power[11, -1]] becomes Rational[7,11]. Why this makes a difference which affects only Raster, and only for large data sets, I don't know.

$\endgroup$
5
$\begingroup$

Simon's method does seem to work, and I cannot explain why either. However, I recommend a different method. I suggest you convert all the exact numbers to machine precision as this typically evaluates much faster. In fact my fix for your problem with the other question was to add N so that the array could be packed, and the same method works here.

Using your original definition for GalColor:

dat = RandomReal[{0, 1}, {512, 512}];

Graphics[Raster[dat, ColorFunction -> N@GalColor]]

et voila:

Mathematica graphics

A two character fix. How about that? :-)

$\endgroup$
3
  • $\begingroup$ ah ! the only reason I was using rationals is to make the function more compact... $\endgroup$
    – chris
    Mar 16, 2013 at 19:28
  • $\begingroup$ Had a similar problem, and this helped just magically. Thank you! $\endgroup$ Sep 19, 2013 at 20:36
  • $\begingroup$ @Alexey Glad I could help. :-) $\endgroup$
    – Mr.Wizard
    Sep 19, 2013 at 20:37

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.