# Plotting Probability Distribution of Data

**URL:** https://discourse.julialang.org/t/plotting-probability-distribution-of-data/99976
**Category:** Visualization
**Tags:** pyplot, plots, statsplots
**Created:** [June 6, 2023, 10:54pm UTC](https://discourse.julialang.org/t/plotting-probability-distribution-of-data/99976 "2023-06-06T22:54:44Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![optimist](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/optimist/32/14797_2.png) [@optimist](https://discourse.julialang.org/u/optimist)
#### Post date: [June 6, 2023, 10:54pm UTC](https://discourse.julialang.org/t/plotting-probability-distribution-of-data/99976/1 "2023-06-06T22:54:44Z")

</div>

I am trying to produce a plot of probability distribution function of some data which is a result of simulations. Using `density` function from `StatPlots` is showing some weird y-axis numbers. I would expect it to be ranging from 0 to 1 as these are probabilities, but it shows numbers 1e-8 to 1e-7. Here is the code

```julia
using StatsPlots
density(rand(0:1e7,10000), xlabel = "Loss", ylabel = "Probability")

```

A plot of CDF does show the right scaling for probabilities.

```julia
ecdfplot(rand(1000:1e7,10000), xlabel = "Loss", ylabel = "
Cumulative Probability")

```

Is there a way to plot probability distribution function of empirical or simulated data in away that shows the scale going up to 1 for probabilities on y-axis?

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [June 6, 2023, 11:20pm UTC](https://discourse.julialang.org/t/plotting-probability-distribution-of-data/99976/2 "2023-06-06T23:20:05Z")

</div>

The density plot is correct:

1. Density considers observations x\_1, \ldots, x\_n as continuous and estimates their probability density function p(x). Note that the probability density is not a probability, but needs to be integrated over some interval (or more generally measurable set) in order to get a probability. In particular, for a sufficiently peaked distribution the density can exceed 1 (see example below).

\mathbb{P}(X \in [a, b]) = \int\_a^b p(x) dx

1. Your data has a wide support from 0 to 10^7 and accordingly the (uniform) density will be around 10^{-7} corresponding to a total probability mass of one, i.e., when integrating the density over the whole support: \mathbb{P}(X \in [0, 10^7]) = 1 = \int\_0^{10^7} 10^{-7} dx.

```julia
julia> using Distributions

julia> density(rand(Normal(0, 0.1), 10000)) # Density will exceed 1

```

In case of discrete observations, you may want to look at histograms or simply compute frequencies, i.e.,

```julia
julia> fs = frequencies(rand(0:1000, 10000));

julia> bar(collect(keys(fs)), values(fs) ./ sum(values(fs)))

```

---

<div class="post-metadata">

### Author: ![optimist](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/optimist/32/14797_2.png) [@optimist](https://discourse.julialang.org/u/optimist)
#### Post date: [June 7, 2023, 8:42am UTC](https://discourse.julialang.org/t/plotting-probability-distribution-of-data/99976/3 "2023-06-07T08:42:40Z")

</div>

Thank you. There is no function `frequencies` so when I run your suggestion I get an error.

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [June 7, 2023, 5:45pm UTC](https://discourse.julialang.org/t/plotting-probability-distribution-of-data/99976/4 "2023-06-07T17:45:26Z")

</div>

Sorry, seems I had `Flux` loaded where its a deprecated function.  
Here is a full example showing all libraries and using `countmap` from `StatsBase` as an alternative:

```julia
julia> using StatsBase

julia> fs = countmap(rand(0:1000, 10000));

julia> using Plots

julia> bar(collect(keys(fs)), values(fs) ./ sum(values(fs)))

```
