1
$\begingroup$

I have a problem specifying the PlotRange for the DensityHistogram plot if Method ->{DistributionAxes ->"Histogram"} is given.

The behaviour I would like to see is the following:

DensityHistogram[
 Partition[
  Riffle[RandomReal[NormalDistribution[0, 1], 1000], 
   RandomReal[NormalDistribution[0, 1], 1000]], 2], {{1}, {1}}, 
 Method -> {"DistributionAxes" -> "Histogram"}, 
 PlotRange -> {{0, 4}, {0, 4}}]

But if I change the range of the input data the behaviour does change in an unexpected way (at least for me) as there is some white space added at the y-axis.

DensityHistogram[
 Partition[
  Riffle[RandomReal[NormalDistribution[0, 100], 1000], 
   RandomReal[NormalDistribution[0, 1], 1000]], 2], {{1}, {1}}, 
 Method -> {"DistributionAxes" -> "Histogram"}, 
 PlotRange -> Automatic]

PlotRange automatic

Even stranger the plot does not adhere the PlotRange anymore if it is explicitly given.

DensityHistogram[
 Partition[
  Riffle[RandomReal[NormalDistribution[0, 100], 1000], 
   RandomReal[NormalDistribution[0, 1], 1000]], 2], {{1}, {1}}, 
 Method -> {"DistributionAxes" -> "Histogram"}, 
 PlotRange -> {{0, 200}, {0, 5}}]

Does anybody have an idea what is happening and maybe how to workaround? (I am on Mathematica 10.4)

$\endgroup$
  • $\begingroup$ You can do Show[DensityHistogram[], PlotRange -> {{0, 200}, {0, 5}}] $\endgroup$ – Feyre Oct 25 '16 at 14:35
  • $\begingroup$ Thank you! That solves the PlotRange but the histogram will be cut. This then can be accounted for by using PlotRangePadding. $\endgroup$ – Freeman Oct 25 '16 at 20:55
1
$\begingroup$

Feyre proposed to use Show[] to fix the output of DensityHistogram[]. PlotRange together with PlotRangePadding will then do the job.

As DensityHistogram[] was planed to be used in an automated script, tuning PlotRange and PlotRangePadding by hand is not possible.

I wrote a small function which does the fixing by extracting the relevant information from a plot as explained by tomd1. This fixes my problem but I am not sure how reliable this is (good enough for me). It will work if the original PlotRange was specified as Automatic. Further I suspect that specifying an Inset[] will break this function. I doubt also that the Automatic definition of the lower bound of PlotRange will work in all cases.

CorrectPlotRange[PlotData_] := Module[{SideHist, Result, SizeX, SizeY, PointData, RangeX, RangeY},

  SideHist = Cases[InputForm[PlotData] , Inset[___], Infinity];
  SizeX = SideHist[[1, 4, 2]]*1.5;
  SizeY = SideHist[[2, 4, 1]]*1.5;
  PointData = Cases[Cases[InputForm[PlotData], RectangleBox[___], Infinity], {_?NumericQ, _?NumericQ}, Infinity];
  RangeX = MinMax@PointData[[All, 1]];
  RangeY = MinMax@PointData[[All, 2]];

  Result = Show[
    PlotData,
    PlotRange -> {{Automatic, RangeX[[2]]}, {Automatic, RangeY[[2]]}},
    PlotRangePadding -> {{SizeY, Scaled[0.01]}, {SizeX, Scaled[0.01]}}

  ];
  (*{Result,SizeX,SizeY,RangeX,RangeY}*)
  Result
];

DensityHistogram[
 Partition[Riffle[RandomReal[NormalDistribution[0, 1000], 1000],RandomReal[NormalDistribution[0, 1], 1000]], 2], 
 {{1}, {2}}, 
 Method -> {"DistributionAxes" -> "Histogram"}, 
 PlotRange -> Automatic]
CorrectPlotRange[%]

enter image description here

This will fix the output of DensityHistogram[] but I am still curious what the actual problem is.

$\endgroup$

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.