# Plotting a line histogram

**URL:** <https://discourse.julialang.org/t/plotting-a-line-histogram/38728>\
**Category:** New to Julia\
**Tags:** question, plotting\
**Created:** [May 4, 2020, 9:09am UTC](https://discourse.julialang.org/t/plotting-a-line-histogram/38728 "2020-05-04T09:09:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 4, 2020, 9:09am UTC](https://discourse.julialang.org/t/plotting-a-line-histogram/38728/1 "2020-05-04T09:09:10Z")

</div>

Hi, I’m trying to plot an approximation of a probability density function (as a line plot) from a list of samples. Is this supported by Plots.jl?

There is the `:stephist` series type, but it makes steps rather than a line plot:

```julia
values = rand(Normal(), 100000);

plot(values, seriestype=:stephist, size=(600,200))

```

![stephist](https://global.discourse-cdn.com/julialang/original/3X/f/e/fefef85c5634137f68fa488c5aa329f785a200a7.png)

There is `:scatterhist` which is closer to what I want, but I couldn’t find how to make it show a line:

```julia
plot(values, seriestype=:scatterhist, linestyle=:solid, size=(600,150))

```

![scatterhist](https://global.discourse-cdn.com/julialang/original/3X/f/3/f34e518a500e7759183569cc3fdfec1a5635140f.png)

I can do it manually:

```julia
using StatsBase
h = fit(Histogram, values, nbins=100)
r = h.edges[1]
x = first(r)+step(r)/2:step(r):last(r)
plot(x, h.weights, size=(600,150))

```

![manual](https://global.discourse-cdn.com/julialang/original/3X/7/1/711f9e739d22cb330027cd7bc89eccee901100e9.png)

But is there a way to do it directly with Plot.jl? For simplicity, and to take advantage of Plot.jl’s smart bin selection…

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [May 4, 2020, 9:25am UTC](https://discourse.julialang.org/t/plotting-a-line-histogram/38728/2 "2020-05-04T09:25:31Z")

</div>

I think what you are looking for is either a fit (like a Gaussian fit in this case), see one of my older posts: [Fitting a 1D distribution using Gaussian Mixtures](https://discourse.julialang.org/t/fitting-a-1d-distribution-using-gaussian-mixtures/28113) or a kernel density estimation [https://github.com/JuliaStats/KernelDensity.jl](https://github.com/JuliaStats/KernelDensity.jl)

This task should not be done by a plotting library if you ask me 😉

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 4, 2020, 9:43am UTC](https://discourse.julialang.org/t/plotting-a-line-histogram/38728/3 "2020-05-04T09:43:54Z")

</div>

Thanks for pointing KernelDensity.jl, that looks quite useful.

Here however I’m not trying to fit a model, it’s really about generating random numbers from an arbitrary distribution and showing what the density looks like. It’s for an introductory lesson in probability, I’d like to keep things as basic as possible (the Julia code in particular should be as pedestrian as possible 🙂 ).

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [May 4, 2020, 9:45am UTC](https://discourse.julialang.org/t/plotting-a-line-histogram/38728/4 "2020-05-04T09:45:48Z")

</div>

Maybe I miss the point, but I would not teach students to artificially smoothen data of a distribution 🙈 they should rather learn why it’s not smooth etc.  
…but as said, I don’t know what you are up to.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 4, 2020, 9:50am UTC](https://discourse.julialang.org/t/plotting-a-line-histogram/38728/5 "2020-05-04T09:50:59Z")

</div>

Maybe:

```julia
julia> using StatsPlots

julia> density(randn(10_000))

```

![image](https://global.discourse-cdn.com/julialang/original/3X/2/1/2125a449fa0c4fc9c2cc822a854440bf347cfa72.png)

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 4, 2020, 9:51am UTC](https://discourse.julialang.org/t/plotting-a-line-histogram/38728/6 "2020-05-04T09:51:05Z")

</div>

It’s just not the subject of that particular lesson (ideally I would do without sampling, showing “perfect” curves). But I agree with the general point.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 4, 2020, 9:52am UTC](https://discourse.julialang.org/t/plotting-a-line-histogram/38728/7 "2020-05-04T09:52:25Z")

</div>

Oh and if by “perfect curves” you mean theoretical pdfs, StatsPlots also has receipes for Distributions:

```julia
julia> using Distributions, StatsPlots

julia> plot(Normal())

```

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

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 4, 2020, 10:19am UTC](https://discourse.julialang.org/t/plotting-a-line-histogram/38728/8 "2020-05-04T10:19:16Z")

</div>

Thanks, `StatsPlots.density` is perfect for the job!

For the theoretical pdfs: yep that’s what I meant (except I’m looking at functions of several random variables, so the distribution is not available in Distributions.jl).

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 4, 2020, 12:29pm UTC](https://discourse.julialang.org/t/plotting-a-line-histogram/38728/9 "2020-05-04T12:29:21Z")

</div>

A histogram is already an artificial way of de-smoothing a distribution.

---

<div class="post-metadata">

**Author:** ![Robert\_Moss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robert_moss/32/48004_2.png) [@Robert\_Moss](https://discourse.julialang.org/u/Robert_Moss)\
**Post date:** [January 9, 2021, 12:09am UTC](https://discourse.julialang.org/t/plotting-a-line-histogram/38728/10 "2021-01-09T00:09:05Z")

</div>

You can also plot a histogram and overlay the density as a line without using `StatsPlots`:

```julia
using Distributions, Plots

dist = Normal(0, 1)
data = rand(dist, 1000)
histogram(data, normalize=true)
plot!(x->pdf(dist, x), xlim=xlims())

```

![image](https://global.discourse-cdn.com/julialang/original/3X/e/a/ea870d7ac375ee870e66e972556821c1105cc810.png)
