# Extend density plot line (along 0)

**URL:** <https://discourse.julialang.org/t/extend-density-plot-line-along-0/67561>\
**Category:** Visualization\
**Tags:** statsplots\
**Created:** [September 2, 2021, 9:54am UTC](https://discourse.julialang.org/t/extend-density-plot-line-along-0/67561 "2021-09-02T09:54:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [September 2, 2021, 9:54am UTC](https://discourse.julialang.org/t/extend-density-plot-line-along-0/67561/1 "2021-09-02T09:54:59Z")

</div>

If I write

```julia
using StatsPlots
density(sqrt.(rand(10000)))
density!(sqrt.(2*rand(10000)))

```

then I get:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/2/02e711f32acda436a9d64df90eac9dd9f9f9621d.png)

The plot would be nicer if the blue line extended all the way to the maximum/minimum x values (along 0). Is there an option for this?

---

<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:** [September 2, 2021, 10:20am UTC](https://discourse.julialang.org/t/extend-density-plot-line-along-0/67561/2 "2021-09-02T10:20:10Z")

</div>

If I understand you correctly you want `density` to pad its estimate with zeros up to some maximum value which is determined by a different call to `density`? I don’t think there’s an option for this and I’m not even sure how that would work in practice (what if you plot a third density? would the first two plots have to be extended accordingly?) so you need to do it manually by adding something like

```julia
julia> plot!(1.1:0.01:1.6, zeros(length(1.1:0.01:1.6)), color = 1, label = "")

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 2, 2021, 12:04pm UTC](https://discourse.julialang.org/t/extend-density-plot-line-along-0/67561/3 "2021-09-02T12:04:09Z")

</div>

Instead of inventing new data, you could force the lines to land on the x-axis at y=0.  
In this case, just using the argument `widen=false` would do the trick.

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [September 2, 2021, 3:11pm UTC](https://discourse.julialang.org/t/extend-density-plot-line-along-0/67561/4 "2021-09-02T15:11:10Z")

</div>

Not sure about the `widen=false`, I tried it but didn’t really seem to work (I increased `lw` to make things clearer)

```julia
using StatsPlots
density(sqrt.(rand(10000)),widen=false,lw=4)
density!(sqrt.(2*rand(10000)),lw=4)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/5/7/575bcbb058bc0be1a63fdec9e933a0404f3669c2.png)  
(I tried putting the `widen=false` in various places, and `=true`, in case I misunderstood something, but with no success)

@nilshg I don’t need it to predict the length of the padding according to the previous call, I agree that this would be weird. But an option like

```julia
using StatsPlots
density(sqrt.(rand(10000)),extend=(0.,1.5)
density!(sqrt.(2*rand(10000)))

```

which then continues the line to the given interval (even if just 0) is what I would be looking for (I’d could then manually set it to whatever I would require). If there’s nothing like that though, trying to stitch things together like you suggest, with separate lines, seem like it might produce an acceptable result.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 2, 2021, 6:01pm UTC](https://discourse.julialang.org/t/extend-density-plot-line-along-0/67561/5 "2021-09-02T18:01:46Z")

</div>

To me it looks good. But some people [see a hat, while others a boa and an elephant](https://www.researchgate.net/figure/My-drawing-was-not-a-picture-of-a-hat-It-was-a-picture-of-a-boa-constrictor-digesting_fig1_336197560).
