Can anyone explain this (to me) odd behavior. If I run this code once (fresh kernel) it works as intended.
data = Table[Random[Real], {i, 5}, {j, 5}];
mycf[x_] := (AppendTo[hdat, x]; Hue[x])
hdat = {};
Show[Graphics[Raster[data, ColorFunction -> mycf]]]
Print["hdat len", Length[hdat]]
Histogram[hdat]
If I execute the same thing a second time the plot is generated but the hdat
array is left empty. It seems some sort of caching is happening??
Edit - an even smaller example to illustrate the issue:
mycf[x_] := (value = 1; Hue[0])
value = 0
Show[Graphics[Raster[{{0}}, ColorFunction -> mycf]]]
value
------------next cell
value
Further clue... it turns out that any ColorFunction
specified gets wrapped in Dynamic[]
:
If you do:
Graphics[Raster[{{0}}]]
Graphics[Raster[{{0}}, ColorFunction -> (Black &)]]
with dynamic updating disabled the first form works while the second does not.
With that in mind, following Show[]
in my example with FinishDynamic[]
seems to do the trick.
Pause[.05]
in between theGraphics
andHistogram
and it worked as you would expect. $\endgroup$Sow[]
/Reap[]
instead ofAppendTo[]
myself... $\endgroup$