4
$\begingroup$

I'm experiencing a small formating glitch with an histogram. I want to define an histogram with a square frame, with precise vertical ticks from 0 to 10. But I'm still getting a larger range, and I don't know why. Here's a working code which shows the problem :

velocity[n_, r_] := velocity[n, r] = RandomReal[{0, 1}]
ca[n_, r_] := ca[n, r] = RandomReal[{-1, 1}]
phi[n_, r_] := phi[n, r] = RandomReal[{0, 2 Pi}]

V0[n_, r_] := V0[n, r] = velocity[n, r] {
        Sqrt[1 - ca[n, r]^2] Cos[phi[n, r]],
        Sqrt[1 - ca[n, r]^2] Sin[phi[n, r]],
        ca[n, r]
    }

Manipulate[
    Histogram[ 
    Table[Norm[V0[n, r]], {n, 1, Np}], {0.05},
    ColorFunction -> Function[{height}, Opacity[height]],
    ChartStyle -> Purple,
    PlotRange -> {{0, 1}, {0, 10}},
    AspectRatio -> 1,
    Axes -> False,
    Frame -> True,
    GridLines -> Automatic,
    FrameLabel -> {
        Style["Velocity", 14],
        Style["Number of particles", 14]
    },
    ImageSize -> {500, 500}
    ],
    {{Np, 1, Style["N", 14, Italic, FontFamily -> "Times"]}, 1, 100, 1},
    Delimiter,
    Row[{
        Button[Style["Randomizer", Bold, Red, 12], {r = RandomReal[]},
        Appearance -> "Palette",
        ImageSize -> {100, 24}
    ]
    }],
    ControlPlacement -> Bottom,
    FrameMargins -> None
]

So what am I doing wrong with this code ?

$\endgroup$
19
  • 3
    $\begingroup$ Is PlotRangePadding -> 0 the answer? $\endgroup$
    – Kuba
    Commented Apr 10, 2016 at 20:14
  • $\begingroup$ @Kuba, nope, I'm still getting the same vertical range, from 0 to approx 11. $\endgroup$
    – Cham
    Commented Apr 10, 2016 at 20:16
  • 2
    $\begingroup$ I solved the issue ! Putting the Histogram inside a Show[...] apparently solves completely the output problem. $\endgroup$
    – Cham
    Commented Apr 11, 2016 at 2:15
  • 1
    $\begingroup$ @Cham That is... interesting behavior :-) perhaps you could post a quick self answer indicating the problems and your workaround, so your question isn't left hanging. $\endgroup$
    – MarcoB
    Commented Apr 11, 2016 at 5:36
  • 1
    $\begingroup$ @N.J.Evans, the problem is its unpredictable. I need a precise setup for a Manipulate. $\endgroup$
    – Cham
    Commented Apr 11, 2016 at 14:08

1 Answer 1

2
$\begingroup$

I solved the issue by wrapping the Histogram inside a Show ! Here's an example which works very well with Mathematica 7 :

(*This code produces the issue :  too much space above the vertical axes : *)
Histogram[
    RandomReal[{0, 10}, 100],
    PlotRange -> {0, 30},
    PlotRangePadding -> None,
    Frame -> True
]

(* This one solves the problem : *)
Show[
    Histogram[RandomReal[{0, 10}, 100]],
    PlotRange -> {0, 30},
    PlotRangePadding -> None,
    Frame -> True
]

Weird, isn't ? The PlotRangePadding -> None is essential here.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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