# Log scale in histogram

**URL:** https://discourse.julialang.org/t/log-scale-in-histogram/65723
**Category:** General Usage
**Tags:** plotting
**Created:** [August 3, 2021, 6:47am UTC](https://discourse.julialang.org/t/log-scale-in-histogram/65723 "2021-08-03T06:47:49Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)
#### Post date: [August 3, 2021, 6:47am UTC](https://discourse.julialang.org/t/log-scale-in-histogram/65723/1 "2021-08-03T06:47:49Z")

</div>

I don’t mind specifying my histogram bins manually in order to get a log scale, but it appears to break the viewport in Plots.jl:

```julia
using Plots
x = rand(1000)
b = 10.0 .^ (-5:0)
histogram(x, bins=b)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/4/b/4b37db01a7d1f6d0db5bb64faf1a1a305a4986db.png)

And setting the x-axis to a log scale is even worse:

```julia
histogram(x, bins=b, xscale=:log10, yscale=:identity)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/0/0/007c03f10cf5f74d9beb67c82eb3b51a0c152367.png)

What can I do about this?

---

<div class="post-metadata">

### Author: ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)
#### Post date: [August 3, 2021, 7:52am UTC](https://discourse.julialang.org/t/log-scale-in-histogram/65723/2 "2021-08-03T07:52:19Z")

</div>

First solution: Use `norm = true` (pdf-like histogram)

```julia
using Plots
x = rand(10^6)
b = 10.0 .^ (-5:0)
histogram(x; alpha=0.3, label="", bin=b, norm=true)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/8/3815605878641a0de9b32efeefc87d4e25acf121.jpeg)

Second solution: Manually use `StatsBase.fit(Histogram{Float64}, x, b)`.

```julia
using Plots, StatsBase
x = rand(10^6)
b = 10.0 .^ (-5:0)
h = fit(Histogram{Float64}, x, b)
h.weights ./= diff(h.edges[1])
plot(h; alpha=0.3, label="")

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/7/27d25c260e7dcf8f16100e1b107682648e30f071.jpeg)

Another example:

```julia
using Plots, StatsBase, Random
X = 3randexp(10^6)
bin = [0; [2^(k/2) - 2^(-1/2) for k in 0:8]]
@show round.(bin; digits=2)
h = fit(Histogram{Float64}, X, bin)
h.weights ./= diff(h.edges[1])
plot(h; alpha=0.3, label="")

```

```julia
round.(bin; digits = 2) = [0.0, 0.29, 0.71, 1.29, 2.12, 3.29, 4.95, 7.29, 10.61, 15.29]

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/a/8af0af9865cf6c6b63b971a1e1d9b213fd175293.jpeg)

---

<div class="post-metadata">

### Author: ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)
#### Post date: [August 3, 2021, 8:14am UTC](https://discourse.julialang.org/t/log-scale-in-histogram/65723/3 "2021-08-03T08:14:06Z")

</div>

Thank you for these suggestions. What about getting a log scale on the x axis?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 3, 2021, 8:15am UTC](https://discourse.julialang.org/t/log-scale-in-histogram/65723/4 "2021-08-03T08:15:45Z")

</div>

It seems to work using Plots.jl pyplot() backend and the `xaxis` keyword:

```julia
using Plots; pyplot()
x = rand(1000)
b = 10.0 .^ (-5:0)
# histogram(x, bins=b) # works OK
histogram(x, bins=b, xaxis=(:log10, (0.001, 10)), yscale=:identity)

```

![Plots_pyplot_xaxis_log_scale](https://global.discourse-cdn.com/julialang/original/3X/2/c/2c75e6a066dbb8b6a1d2aaff4fa54ce84a5b4e86.png)

---

<div class="post-metadata">

### Author: ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)
#### Post date: [August 3, 2021, 9:34am UTC](https://discourse.julialang.org/t/log-scale-in-histogram/65723/5 "2021-08-03T09:34:01Z")

</div>

> [@maxkapur](#):
>
> What about getting a log scale on the x axis?

It seems to work well if you set `xlim=extrema(b)` as well as `xscale=:log10`.

```julia
using Plots
n = 10^6
f(x) = n * x * log(10)
x = rand(n)
b = 10.0 .^ (-5:0)
histogram(x; alpha=0.3, label="", bin=b, xscale=:log10, xlim=extrema(b))
x = 10.0 .^ (-5:0.01:0)
plot!(x, f; label="n × pdf on log-scaled xaxis", legend=:topleft)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/a/da881496b53b0be57abec1ca1b5b7133101a50da.jpeg)

```julia
using Plots
n = 10^6
f(x) = n * x * log(10)
x = rand(n)
b = 10.0 .^ (-5:0.2:0)
histogram(x; alpha=0.3, label="", bin=b, xscale=:log10, xlim=extrema(b))
x = 10.0 .^ (-5:0.01:0)
plot!(x, f; label="n × pdf on log-scaled xaxis", legend=:topleft)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/7/075084f714ac9128e630c8a7c1fe788964cde9ee.jpeg)

```julia
using Plots, StatsBase
n = 10^6
f(x) = n * x * log(10)
x = rand(n)
b = 10.0 .^ (-5:0.2:0)
h = fit(Histogram{Float64}, x, b)
h.weights ./= diff(log10.(h.edges[1]))
plot(h; alpha=0.3, label="", xscale=:log10, xlim=extrema(b))
x = 10.0 .^ (-5:0.01:0)
plot!(x, f; label="n × pdf on log-scaled xaxis", legend=:topleft)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/4/94a4897ecca89dd412a45117b4526c11c0edf0d9.jpeg)

Another example

```julia
using Plots, StatsBase, Random
n = 10^6
f(x) = n * exp(-x/3)/3 * x * log(10)
X = 3randexp(10^6)
bin = [1e-3; [2^(k/2) - 2^(-1/2) for k in 0:8]]
@show round.(bin; digits=2)
h = fit(Histogram{Float64}, X, bin)
h.weights ./= diff(log10.(h.edges[1]))
plot(h; alpha=0.3, label="", xscale=:log10, xlim=extrema(bin))
x = exp.(range(log.(extrema(bin))...; length=500))
plot!(x, f; label="n × pdf on log-scaled xaxis", legend=:topleft)

```

```julia
round.(bin; digits = 2) = [0.0, 0.29, 0.71, 1.29, 2.12, 3.29, 4.95, 7.29, 10.61, 15.29]

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/9/89f798585750974e15542119b328ed6c811adc62.jpeg)

Edit: Update examples.

---

<div class="post-metadata">

### Author: ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)
#### Post date: [August 3, 2021, 11:26pm UTC](https://discourse.julialang.org/t/log-scale-in-histogram/65723/6 "2021-08-03T23:26:13Z")

</div>

There’s an _extrema_ function? God, if I owed you a nickle for every time I have typed `xlim=(min(x), max(x))`…

Thank you for the tip.
