# How to extract (absolute) Tick and/or PlotRange specs from output of Histogram?

If, for example,

g = Histogram[RandomVariate[NormalDistribution[0, 1], 200]];


...I want to extract an absolute (abscissa) Tick specification from g. (The RHS above is just a convenient example, straight from the docs for Histogram.)

FullGraphics and AbsoluteOptions are basically useless for this.

I also tried to extract an absolute PlotRange spec from g, but, again, nothing I tried worked. (The idea here was that, maybe, I could pass the extracted PlotRange spec to a suitable dummy Plot expression from whose output I may be able to extract an absolute abscissa Tick spec from.)

• After examination of FullForm[g]: the PlotRange can be extracted with Cases[g, x : Rule[PlotRange, ___] :> x, Infinity]. The Rules for Ticks and FrameTicks are, however, expressed in terms of Automatic. Interestingly, AbsoluteOptions[g, PlotRange] gives the PlotRange in the error message, but not as output, though. – corey979 Dec 4 '16 at 19:02

I also tried to extract an absolute PlotRange spec from g:

SeedRandom[1]
dd = RandomVariate[NormalDistribution[0, 1], 200];
g = Histogram[dd]


1. Use ChartingCommonDumpgetplotrange with Options[g, AxesOrigin]:

prF1 = ChartingCommonDumpgetplotrange[#, AxesOrigin /. Options[#, AxesOrigin]] &;

prF1 @g


{{-2.5, 3.}, {0, 45.}}

2. Post-process the box expressions of g to extract the bounding boxes:

ClearAll[prF2]
prF2 = Through[{Min, Max}@#] & /@ Transpose[
Join @@ Cases[# // ToBoxes, RectangleBox[x_, y_, ___] :> {x, y}, ∞]] &;

prF2 @ g


{{-2.5, 3.}, {0, 45}}

Legended[Show[g, GridLines -> prF1[g], GridLinesStyle -> Red,
Axes -> False, Method -> {"GridLinesInFront" -> True},
Frame -> True, FrameTicks -> Thread[{Reverse@prF1[g], None}]],
Panel[TableForm[prF1[g], TableHeadings -> {{"x", "y"}, {"min", "max"}}],
"plot range", Top]]


3. Inject ChartingChartStyleInformation["BoundingBox"] into ChartElementFunction option setting:

ClearAll[prF3]
prF3 = Module[{boundingbox}, Histogram[#, ChartElementFunction ->
((boundingbox = ChartingChartStyleInformation["BoundingBox"];
ChartElementData["Rectangle"][##]) &)]; boundingbox] &;

prF3[dd]


{{-2.5, 3.}, {0, 45}}

4. Use Chartingget2DPlotRange[g] and Options[g, PlotRangePadding] to compute the plot range:

The function Chartingget2DPlotRange gives padded plot range (including PlotRangePadding values):

Chartingget2DPlotRange[g]


{{-2.61, 3.11}, {-0.9, 49.5}}

The value of the option PlotRangePadding is easily obtained:

Options[g, PlotRangePadding]


Given padded plot range and padding information we can solve for the value of plot range:

paddingsF = Module[{i = 1}, (-1)^(i++) # & @@@ #] & /@

prF4 = Module[{aa = Array[a, {2, 2}]},
Chop[aa /. First@Solve[aa + (First /@ Differences /@ aa) paddingsF[#] ==
Chartingget2DPlotRange[#], Flatten[aa], Reals]]] &;

prF4 @ g


{{-2.5, 3.}, {0, 45}}

Note: The last method works in version 9, but not in version 11.

For the PlotRange, you can use my function graphicsInformation:

SeedRandom[1];
g = Histogram[RandomVariate[NormalDistribution[0, 1], 200]]

pr = "PlotRange" /. graphicsInformation[g]


{{-2.61458, 3.11458}, {-0.967742, 47.4194}}

As you say, using AbsoluteOptions to obtain the Ticks doesn't work:

Ticks /. AbsoluteOptions[g]


PlotRange::prng: Value of option PlotRange -> {{-2.5,3.},{All,All}} is not All, Full, Automatic, a positive machine number, or an appropriate list of range specifications.

PlotRange::prng: Value of option PlotRange -> {{-2.5,3.},{All,All}} is not All, Full, Automatic, a positive machine number, or an appropriate list of range specifications.

PlotRange::prng: Value of option PlotRange -> {{-2.5,3.},{All,All}} is not All, Full, Automatic, a positive machine number, or an appropriate list of range specifications.

General::stop: Further output of PlotRange::prng will be suppressed during this calculation.

{Automatic, Automatic}

Base on the error message, we can fix this by inserting the PlotRange manually:

Ticks /. AbsoluteOptions[Show[g, PlotRange->pr]] //Short


{{{-2.,-2.,{0.00625,0.},{,AbsoluteThickness[0.25]}},<<27>>,{-2.6,,{0.00375,0.},{,AbsoluteThickness[0.125]}}},{<<1>>}}