4
$\begingroup$

I have a histogram that I represent on a base 10 semi-log scale, and I need to fix the plot range. If I set the lower bound for the logarithmic axis, the histogram bins do not stretch downward fully. A minimal working example of the problematic code is:

Histogram[
  RandomVariate[NormalDistribution[1, 3], 10^4],
  Automatic,
  "PDF",
  PlotRange->{All,{-7,0}}, PlotTheme->"Scientific",ScalingFunctions->{None,"Log10"}]

This is what the output looks like:

enter image description here

So you see the bins do not stretch all the way downward.

Thanks!

$\endgroup$
5
  • 1
    $\begingroup$ Seems buggy to me. A quick and dirty fix is something like Histogram[...] /. Rectangle[{x1_, _}, rest___] :> Rectangle[{x1, -7}, rest] to replace the bottom coordinates of all the bars with the correct value. $\endgroup$
    – Lukas Lang
    Sep 30, 2022 at 12:08
  • $\begingroup$ @LukasLang Thanks for the answer! However, this does not work, at least not for me. It even seems to reflect the bins about the y-axis. Does it work on your machine? $\endgroup$
    – JonasB
    Sep 30, 2022 at 13:35
  • $\begingroup$ I am not sure that PlotRange -> {All, {-7, 0}} makes sense to me. Shouldn't you specify the plot range according to the original values, rather than their logarithm, i.e. PlotRange -> {All, {1*^-7, 1}}? Your version doesn't work well in MMA 12.3.1 on Win10-64. Having said that, changing the plot range doesn't remove your problem, but setting it to Automatic, or using @Lukas' proposed workaround works for me. $\endgroup$
    – MarcoB
    Sep 30, 2022 at 14:53
  • $\begingroup$ @JonasB which version of Mathematica are you using? $\endgroup$
    – Lukas Lang
    Sep 30, 2022 at 14:55
  • $\begingroup$ @LukasLang I'm using MMA 10.1. The thing is, I need the plotrange to be fixed at these values, since I will compare different histograms on the same scale, and the one with the lowest value has these ranges. So I cannot just omit the PlotRange command and let MMA determine it automatically $\endgroup$
    – JonasB
    Oct 1, 2022 at 7:25

1 Answer 1

2
$\begingroup$

With v13.1 it is necessary to omit specifying the PlotRange

$Version

(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)

Clear["Global`*"]

SeedRandom[1234];

Histogram[
 RandomVariate[NormalDistribution[1, 3], 10^4],
 Automatic, "PDF",
 PlotRange -> {All, {-7, 0}},
 PlotTheme -> "Scientific",
 ScalingFunctions -> {None, "Log10"}]

enter image description here

The above is clearly wrong.

SeedRandom[1234];

Histogram[
 RandomVariate[NormalDistribution[1, 3], 10^4],
 Automatic, "PDF",
 PlotTheme -> "Scientific",
 ScalingFunctions -> {None, "Log10"}]With v13.1 it is necessary to omit specifying the `PlotRange`

enter image description here

EDIT: However, you can specify the PlotRange as either All or Full

$\endgroup$
1
  • $\begingroup$ Thanks for the answer. However, I really need the range to be fixed at 1 and 10^-7, so I cannot let MMA choose it for itself. I'm using MMA 10.1 on an Ubuntu 20 machine. $\endgroup$
    – JonasB
    Oct 1, 2022 at 7:27

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.