# How to plot continuous probability distributions (in Julia)

## Task

There are many famous continuous probability distributions, such as the normal and exponential distributions. How can we get access to them in software, to plot the distribution as a curve?

Related tasks:

- How to generate random values from a distribution
- How to compute probabilities from a distribution
- How to plot discrete probability distributions

## Solution

You can import many different random variables from Julia’s `Distributions`

package.
The full list of them is online here.

If you don’t have that package installed, first run `using Pkg`

and then
`Pkg.add( "Distributions" )`

from within Julia.

The challenge with plotting a random variable is knowing the appropriate sample space, because some random variables have sample spaces of infinite width, which cannot be plotted.

But we can just ask Julia to show us the central 99.98% of a continuous distribution, which is almost always indistinguishable to the human eye from the entire distribution.

We style the plot below so that it is clear the sample space is continuous.

1
2
3
4
5
6
7
8
9

using Distributions
X = Normal( 10, 5 ) # use a normal distribution with μ=10 and σ=5
xmin = quantile( X, 0.0001 ) # compute min x as the 0.0001 quantile
xmax = quantile( X, 0.9999 ) # compute max x as the 0.9999 quantile
xs = range( xmin, xmax, length=100 ) # create 100 x values in that range
using Plots
plot( xs, pdf.( X, xs ) ) # plot the shape of the distribution

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Contributed by Nathan Carter (ncarter@bentley.edu)