density

density(values)

Plot a kernel density estimate of values.

Plot type

The plot type alias for the density function is Density.

Attributes

bandwidth = automatic — Kernel density bandwidth, determined automatically if automatic.

boundary = automatic — Boundary of the density estimation, determined automatically if automatic.

color = @inherit patchcolor — Usually set to a single color, but can also be set to :x or :y to color with a gradient. If you use :y when direction = :x (or vice versa), note that only 2-element colormaps can work correctly.

colormap = @inherit colormapNo docs available.

colorrange = Makie.automaticNo docs available.

colorscale = identityNo docs available.

cycle = [:color => :patchcolor]No docs available.

direction = :x — The dimension along which the values are distributed. Can be :x or :y.

inspectable = @inherit inspectableNo docs available.

linestyle = nothingNo docs available.

npoints = 200 — The resolution of the estimated curve along the dimension set in direction.

offset = 0.0 — Shift the density baseline, for layering multiple densities on top of each other.

strokearound = falseNo docs available.

strokecolor = @inherit patchstrokecolorNo docs available.

strokewidth = @inherit patchstrokewidthNo docs available.

weights = automatic — Assign a vector of statistical weights to values.

Examples

using CairoMakie


f = Figure()
Axis(f[1, 1])

density!(randn(200))

f

using CairoMakie


f = Figure()
Axis(f[1, 1])

density!(randn(200), direction = :y, npoints = 10)

f

using CairoMakie


f = Figure()
Axis(f[1, 1])

density!(randn(200), color = (:red, 0.3),
    strokecolor = :red, strokewidth = 3, strokearound = true)

f

using CairoMakie


f = Figure()
Axis(f[1, 1])

vectors = [randn(1000) .+ i/2 for i in 0:5]

for (i, vector) in enumerate(vectors)
    density!(vector, offset = -i/4, color = (:slategray, 0.4),
        bandwidth = 0.1)
end

f

Gradients

You can color density plots with gradients by choosing color = :x or :y, depending on the direction attribute.

using CairoMakie


months = ["January", "February", "March", "April",
    "May", "June", "July", "August", "September",
    "October", "November", "December"]

f = Figure()
Axis(f[1, 1], title = "Fictive temperatures",
    yticks = ((1:12) ./ 4,  reverse(months)))

for i in 12:-1:1
    d = density!(randn(200) .- 2sin((i+3)/6*pi), offset = i / 4,
        color = :x, colormap = :thermal, colorrange = (-5, 5),
        strokewidth = 1, strokecolor = :black)
    # this helps with layering in GLMakie
    translate!(d, 0, 0, -0.1i)
end
f

Due to technical limitations, if you color the :vertical dimension (or :horizontal with direction = :y), only a colormap made with just two colors can currently work:

using CairoMakie


f = Figure()
Axis(f[1, 1])
for x in 1:5
    d = density!(x * randn(200) .+ 3x,
        color = :y, colormap = [:darkblue, :gray95])
end
f

Using statistical weights

using CairoMakie, Distributions


N = 100_000
x = rand(Uniform(-2, 2), N)

w = pdf.(Normal(), x)

fig = Figure()
density(fig[1,1], x)
density(fig[1,2], x, weights = w)

fig