Mathematica: Retrieving PlotRange from Histogram
data = {-1.2056, -1.46192, -1.30053, -2.52879, -0.99636, -1.73904, \
-1.164, -1.83398, -0.97505, -0.503256, -0.63802, -0.785963, \
-0.711821, -0.820439, -1.8699, -3.9659, -1.4456, -1.67021, -1.42009, \
-2.5644, -1.45002, -1.27806, -1.66529, -1.67073, -3.31102, -3.38638};
hist = Histogram[data, PlotRange -> Automatic]
First[PlotRange /. Options[hist, PlotRange]]
Update: getting PlotRange
in both directions:
hist = Histogram[data, PlotRange -> Automatic, BarOrigin -> Left]
PlotRange /. Options[hist, PlotRange]
{{All, All}, {-4., 0.}}
To get the values for {All, All}
is more involved than suggested in OP:
it will only make sense to get hold of the second value of
PlotRange
(in my example:{-4.,0.}
) since one can calculate the first one for instance through{0, Length[data]}
{0, Length[data]}
{0, 26}
Obviously not the same as the horizontal plot range in the picture above.
To get plot ranges in both horizontal and vertical dimensions, we can use the functions prF1
,prF2
, or prF3
from this answer to a closely related Q/A:
ClearAll[prF1]
prF1 = Charting`CommonDump`getplotrange[#, AxesOrigin /. Options[#, AxesOrigin]] &;
prF1 @ hist
{{0, 14.},{-4., 0.}}
ClearAll[prF2]
prF2 = MinMax/@Transpose[Join@@Cases[ToBoxes@#, RectangleBox[x_, y_, ___] :> {x, y}, ∞]]&;
prF2 @ hist
{{0, 14.}, {-4., 0.}}
prF3 = Module[{boundingbox},
Histogram[#, ChartElementFunction -> ((boundingbox =
Charting`ChartStyleInformation["BoundingBox"];
ChartElementData["GlassRectangle"][##]) &)]; boundingbox] &;
prF3 @ data
{{0, 14.}, {-4., 0.}}
Original answer:
You can also extract the PlotRange
from the second Part
of hist
:
PlotRange /. hist[[2]] // First
(* {-4.`, 0.`} *)
Note: hist[[2]]
contains the options
hist[[2]]
(* {AspectRatio->1/GoldenRatio, Axes->{True,True}, AxesLabel->{None,None},
AxesOrigin->{-4.,0}, FrameTicks->{{Automatic, Automatic},{Automatic, Automatic}},
GridLines->{None,None}, PlotRange->{{-4.,0.},{All,All}},
PlotRangePadding->{{Scaled[0.02],Scaled[0.02]},
{Scaled[0.02],Scaled[0.1]}},Ticks->{Automatic,Automatic}} *)
You could use my function graphicsInformation to do this:
data = {-1.2056,-1.46192,-1.30053,-2.52879,-0.99636,-1.73904,-1.164,
-1.83398,-0.97505,-0.503256,-0.63802,-0.785963,-0.711821,-0.820439,
-1.8699,-3.9659,-1.4456,-1.67021,-1.42009,-2.5644,-1.45002,-1.27806,
-1.66529,-1.67073,-3.31102,-3.38638};
hist=Histogram[data,PlotRange->Automatic]
graphicsInformation[hist]
{"ImagePadding" -> {{13.7242, 1.5}, {12.7383, 0.5}}, "ImageSize" -> {360., 226.322}, "PlotRangeSize" -> {344.776, 213.083}, "ImagePaddingSize" -> {15.2242, 13.2383}, "PlotRange" -> {{-4.08333, 0.0833333}, {-0.301075, 14.7527}}}
Note that the PlotRange returned is different than that given by the other answers, e.g.:
First[PlotRange /. Options[hist]]
{-4., 0.}
Looking at the actual plot, it is clear that the value returned by graphicsInformation
is more accurate.