I have the following code:

plot1 = Show[DensityHistogram[data, 100, "Count",
   PlotRange -> {{0, 200}, {0, 200}}, ColorFunction -> "Rainbow",
   ChartLegends -> Automatic
   ], PlotRangePadding -> 0]

enter image description here

which I would like to export as an array of numbers and reproduce the original image with ArrayPlot function. As the range is 200x200, the array should be of the same size to represent the whole range. Is there a way to get this array? This question is consequence of the precedent post Export a density plot as a columns and rows numbers

Thank you for your help

  • 2
    $\begingroup$ whats wrong with HistogramList ? $\endgroup$ – george2079 Sep 12 '17 at 17:04
data = RandomVariate[BinormalDistribution[{10, 10}, {5, 5}, .5], 1000]; 

You can use HistogramList using the same binspec you used in DensityHistogram as the second argument:

{bins, heights} = HistogramList[data, 20]; 

Row[{dp = DensityHistogram[data, 20, "Count", ColorFunction -> "Rainbow", 
    Frame -> True,   ImageSize -> 400], 
  ArrayPlot[Reverse@Transpose@heights, Frame -> True, 
   FrameTicks -> {{Automatic, None}, {Automatic, None}}, 
   DataRange -> PlotRange[dp], ColorRules -> {0 -> White}, 
   ColorFunction -> "Rainbow", ImageSize -> 400]}]

enter image description here

Note the use of option DataRange in ArrayPlot. Without it, ArrayPlot assumes the x and y values are row and column indices of the input array. The correct setting for this option can be obtained in several ways:


{{-4., 26.}, {-6., 24.}}

PlotRange /. Options[dp, PlotRange]

{{-4., 26.}, {-6., 24.}}

Or using bins obtained from HistogramList:

 MinMax /@ bins (* Through[{Min, Max}@#] & /@ bins for version 9 and earlier*)

{{-4, 26}, {-6, 24}}

  • $\begingroup$ If I want to Export the array of count or intensity values but only a selected range, could I use HistogramList[data,{0,200,20},"Count"]? $\endgroup$ – user1993416 Sep 12 '17 at 17:24
  • $\begingroup$ @user1993416, i think it should work. $\endgroup$ – kglr Sep 12 '17 at 17:29

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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