TransWikia.com

Translating Histogram

Mathematica Asked on January 23, 2021

SeedRandom[1];
RandomVariate[NormalDistribution[], 100];
Histogram[%, Automatic, "Probability"]

The above code produces a histogram like

enter image description here

I want to translate the figure by say ${1,1}$ as follows:

enter image description here

Can someone help me do this?

3 Answers

Update: An alternative approach is to use ChartElementFunction:

Histogram[data, Automatic, "Probability", ChartStyle -> "Rainbow",
 ChartElementFunction -> (ChartElementDataFunction["GlassRectangle"][1 + #, ##2] &),
 AxesOrigin -> {-3, 1}, PlotRange -> All]

enter image description here


Original post:

SeedRandom[1];
data = RandomVariate[NormalDistribution[], 100];
hist = Histogram[data, Automatic, "Probability"]

Show[MapAt[Translate[#, {1, 1}] &, hist, {1}], 
 AxesOrigin -> {-3, 1}, PlotRange -> {{-3, 4}, {1, 1.5}}]

enter image description here

Alternatively, post-process Rectangles:

Show[hist /. RectangleBox[{x0_, y0_}, {x1_, y1_}, z___] :> 
             RectangleBox[1 + {x0, y0}, 1 + {x1, y1}, z],
 AxesOrigin -> {-3, 1}, PlotRange -> {{-3, 4}, {1, 1.5}}]
(* same picture *)

Correct answer by kglr on January 23, 2021

To shift left-to-right, merely add $1$ to each sampled point:

SeedRandom[1];
1 + #& /@ RandomVariate[NormalDistribution[], 100];
Histogram[%, Automatic, "Probability"]

If one lists the vertical axis as "probability," then surely one doesn't want to add $1$ to each abscissa value as that would violate the definition of a probability measure.

Answered by David G. Stork on January 23, 2021

You could just re-write the Ticks:

Histogram[mydata, Automatic, "Probability", 
 Ticks -> {{#, # + 1, {0.015, 0}} & /@  Range[-3, 3], 
           {#, # + 1, {0.015, 0}} & /@ Range[0, 0.35, 0.05]}]

enter image description here

Minor ticks are left as an exercise for the reader.

Answered by Verbeia on January 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP