To find the color values used in a plot, I was using Sow
as shown here:
Short@Reap[Histogram3D[RandomVariate[BinormalDistribution[.5], 500],
ColorFunction -> (ColorData["Rainbow"][Sow[#1]] &)]
]
But I get an error if I simply replace Histogram3D
by
Short@Reap[
DensityHistogram[RandomVariate[BinormalDistribution[.5], 500],
ColorFunction -> (ColorData["Rainbow"][Sow[#1]] &)]
]
Now I get the following message:
Transpose::nmtx : "The first two levels of the one-dimensional list"
{{{-3.5,3.},{-3.5,3.},{0.,24.}},0, ... , 1} cannot be transposed.
Transpose::argt : "Transpose called with 0 arguments; 1 or 2
arguments are expected."
It's possible to work around this problem by doing without Sow
and Reap
as follows:
l = {};
{DensityHistogram[RandomVariate[BinormalDistribution[.5], 500],
ColorFunction -> (ColorData["Rainbow"][AppendTo[l, #1]; #1] &)],
N@l}
But it would be nice to know if there is an explanation for why Sow
doesn't work in the DensityHistogram
example whereas it works in Histogram3D
. The same error appears if I leave out the Reap
.
From the message it seems clear that a list of all PlotRange
tuples is being sown at some point before the ColorFunction
actually is put to use. I tried to suppress passing that kind of argument to Sow
by modifying the ColorFunction
to
ColorFunction -> (If[ListQ[#1], White,
ColorData["Rainbow"][Sow[#1]]] &)
but it had no effect. As Leonid pointed out in his answer, there must therefore be a different invocation of Sow
outside the color function.
Maybe someone knows another way to suppress the invalid ColorFunction
call that seems to trip up Sow
. However, it doesn't seem to be obvious because when I don't use Sow
as in my work-around based on AppendTo
with a list l
, there is no entry in the resulting list l
that corresponds to the PlotRange
tuples.
Edit
It seems that this is a problem specifically with DensityHistogram
. I also noticed that the setting for FrameTicks
is ignored by DensityHistogram
. It can be restored by wrapping the plot in Show
with the desired FrameTicks
option.
None of these issues arise with the related function SmoothDensityHistogram
.
Leonid pointed out what the probable cause for the Sow
error is, but since wrapping the result in another Show
, I would for now stick with the workaround I mentioned above: ditch Sow
and Reap
, and use AppendTo
to collect the desired values in a list l
using a CompoundExpression
of the type AppendTo[l, #]; #
.
DensityHistogram
orBinormalDistribution
-- does it affect onlyDensityHistogram
? $\endgroup$DensityHistogram
. You could replaceBinormalDistribution
byNormalDistribution
withRandomvariate
getting argument{500,2}
, but that won't change anything. Sorry - I wasn't trying to rub in your lack of version 8... if I find another case I'll post an update. $\endgroup$Internal`InheritedBlock
, making this effect local to the execution stack insidewithTaggedReapSow
dynamic environment. The rest of the system is unaffected by these modifications. $\endgroup$