# Makie density line plot

**URL:** <https://discourse.julialang.org/t/makie-density-line-plot/63137>\
**Category:** General Usage\
**Tags:** makie\
**Created:** [June 18, 2021, 11:02am UTC](https://discourse.julialang.org/t/makie-density-line-plot/63137 "2021-06-18T11:02:47Z")\
**Posts on this page:** 12\
**Page:** 1

<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:** [June 18, 2021, 11:02am UTC](https://discourse.julialang.org/t/makie-density-line-plot/63137/1 "2021-06-18T11:02:47Z")

</div>

Can I somehow get a `density` as a line plot in Makie?

The best I’ve come up with so far is

```julia
density(randn(1_000), color = (:red, 0.0), strokearound = true, strokecolor = :blue, strokewidth = 2)

```

which seems a bit clumsy (setting the actual plot to zero and then adding the line, its color, and width seperately), and doesn’t quite produce what I’m looking for, as it also draws a line on the x-axis.

Is there a more straightforward way to do this?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [June 18, 2021, 11:04am UTC](https://discourse.julialang.org/t/makie-density-line-plot/63137/2 "2021-06-18T11:04:15Z")

</div>

The line on the x axis is specifically enabled with `strokearound = true`. I guess we could add a type recipe for the KernelDensity object so that it works with lines as well (that could already be the case actually).

---

<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:** [June 18, 2021, 11:07am UTC](https://discourse.julialang.org/t/makie-density-line-plot/63137/3 "2021-06-18T11:07:50Z")

</div>

Yes, I thought so, but didn’t see another option to get the line (I guess I want `strokeabove`).

I’m not sure I understand the second part of your answer - are you saying currently `density` generates a `KernelDensity` object and I might be able to somehow call `lines(kernel_density_instance)` to plot that?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [June 18, 2021, 11:12am UTC](https://discourse.julialang.org/t/makie-density-line-plot/63137/4 "2021-06-18T11:12:39Z")

</div>

Well `strokearound = false` will not stroke around, just above 🙂

---

<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:** [June 18, 2021, 11:15am UTC](https://discourse.julialang.org/t/makie-density-line-plot/63137/5 "2021-06-18T11:15:28Z")

</div>

Ah my apologies! I thought I need to set `strokearound = true` to get any line, and `strokecolor` only sets its color - it hadn’t occurred to me that just setting `strokecolor` (or indeed just `strokewidth` - although that, maybe somewhat surprisingly, produces a black line rather than one with the first color on the current palette) produes the line already!

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [June 18, 2021, 11:15am UTC](https://discourse.julialang.org/t/makie-density-line-plot/63137/6 "2021-06-18T11:15:33Z")

</div>

The other thing I meant was this:

```julia
lines(Makie.KernelDensity.kde(randn(100)))

```

 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/a/5/a5b78ebc9d1efcd6c4e6c5a698c97383cce2b8d0.png)

---

<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:** [June 18, 2021, 11:23am UTC](https://discourse.julialang.org/t/makie-density-line-plot/63137/7 "2021-06-18T11:23:16Z")

</div>

Thanks, another good one. So to sum up, there’s either:

```julia
x = randn(1_000)

density(x, color = (:red, 0.0), strokecolor = :blue, strokewidth = 2)

```

pros: uses `density` function, which makes for clear code; cons: quite verbose as one has to set the fill to zero alpha, and then explicitly set color and strokewidth of the line (which also means color is not automatically picked from palette)

or:

```julia
lines(Makie.KernelDensity.kde(x))

```

pros: more concise, directly plots a line only with the color picked automatically from current palette; cons: slightly less clear code and `kde` function is not exported so relies on internals?

Would there be a case for a `density(x, type = :line)` or similar method that is basically a wrapper around the `lines` solution?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [June 18, 2021, 12:00pm UTC](https://discourse.julialang.org/t/makie-density-line-plot/63137/8 "2021-06-18T12:00:39Z")

</div>

One can of course import `KernelDensity` themselves, that would make it less hacky looking.

If your issue is that the line is not color-cycled, you can fix that by setting `Density = (cycle = [:strokecolor => :color],)` in the theme or just `cycle = ...` in the density call. The default is `[:color => :patchcolor]`.

---

<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:** [June 18, 2021, 12:48pm UTC](https://discourse.julialang.org/t/makie-density-line-plot/63137/9 "2021-06-18T12:48:04Z")

</div>

One other issue I stumbled on actually with the `density` solution: because the plot is constructed from an empty area with a line around it, the legend doesn’t show a line for the plot, but rather a box, i.e.:

```julia
density(x, color = (:red, 0.0), strokecolor = :blue, strokewidth = 2, label = "Test")
axislegend()
current_figure()

```

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

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [June 18, 2021, 12:49pm UTC](https://discourse.julialang.org/t/makie-density-line-plot/63137/10 "2021-06-18T12:49:18Z")

</div>

yeah that’s because I overrode it to look similar to other area plots

---

<div class="post-metadata">

**Author:** ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)\
**Post date:** [July 25, 2026, 9:43pm UTC](https://discourse.julialang.org/t/makie-density-line-plot/63137/11 "2026-07-25T21:43:30Z")

</div>

IMO this is _very inconvenient_. Why oh why can’t the density plot be just a `lines` plot of a kernel density estimate? My use case: multiple potentially overlapping `density` plots are illegible. Also, personal opinion: a density estimate is a function, so plot it like `lineplot`, which (correctly) doesn’t fill the area between the function’s graph and the X axis.

Is there a reason why `density` is an “area plot”? Its area is always 1, there’s nothing interesting about it. [`ecdf`](https://docs.makie.org/stable/reference/plots/ecdf) is not an area plot, why would `density` be an area plot? It’s just the plot of the function f(x) = \frac1N \sum\_{n=1}^N \mathcal N(x; x\_n, h) — is this an area?

I think `hist` shouldn’t be filled either. A histogram is a (step) function, so plot it like a function, like `lineplot`! In other words, `hist` should be the same as [`stephist`](https://docs.makie.org/dev/reference/plots/stephist.html).

If I were to design this part of the Makie API, I’d just make `ecdf` and `hist` plot step functions and make `density` equivalent to a `lineplot` of the corresponding KDE.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [July 26, 2026, 5:59am UTC](https://discourse.julialang.org/t/makie-density-line-plot/63137/12 "2026-07-26T05:59:47Z")

</div>

I’m not sure but it’s the convention I’m most familiar with. You can set color to transparent and strokecolor to be cycled in your theme and density should behave mostly like you want. For the histogram there’s stephist as you note.
