# StatsPlot -- axes labels overwrite each other

**URL:** <https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539>\
**Category:** Visualization\
**Tags:** statsplots\
**Created:** [October 28, 2021, 9:30am UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539 "2021-10-28T09:30:57Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 28, 2021, 9:30am UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/1 "2021-10-28T09:30:57Z")

</div>

I’m plotting some results from a `Turing` session. Using the Turing recipe, I get:

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

The problem is the tiny variation in the sample value, which makes the density plot look ugly/overlapping (numeric) labels along the abscissa of the density plot. Similarly, when I use StatsPlot directly on the result, e.g.,

```julia
fg_tah = density(p_est[:,:Tah0,:],lc=:red,la=1,lw=2,framestyle=:box,xlabel="Sample",
    ylabel="Density",grid=false,label=L"T_{\mathrm{a}}^\mathrm{h}",title="f)");

```

leads to:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/3/132ada840107066388a4905219e62752d460b932.png)

which is virtually unreadable…

Question: what is the simplest way to fix the axes labels?

---

<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:** [October 28, 2021, 9:53am UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/2 "2021-10-28T09:53:56Z")

</div>

This plot parameter ` formatter=:plain` and proper font size should fix it.

```julia
using StatsPlots
gr(dpi=600)
x = LinRange(1.5123,1.51234,100)
y = rand(x,100)
p1 = density(x,y, c=:red, label="problem")
p2 = density(x,y, c=:green, label="no problem", formatter=:plain)
plot(p1, p2, tickfontsize=4, legend=:outertop)

```

 ![StatsPlots_density_fix_ticks](https://global.discourse-cdn.com/julialang/original/3X/d/3/d3d6e7303dab0c0075209bed63ddee7902120412.png)

> **Or to manually control the ticks labels**
>
> ```julia
> using StatsPlots
> x = LinRange(1.5123,1.51234,100)
> y = rand(x,100)
> p1 = density(x,y, c=:red, label="problem")
> x0,x1 = extrema(x)
> dx = x1 - x0
> xt = LinRange(-dx/4 + x0, dx/4 + x1, 5)
> p2 = density(x,y, xticks = (xt, round.(xt, digits=6)), xlims=(-dx/2 + x0, dx/2 + x1), c=:green, label="no problem")
> plot(p1, p2, tickfontsize=5, legend=:outertop)
> 
> ```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 28, 2021, 10:56am UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/3 "2021-10-28T10:56:22Z")

</div>

Thanks a lot!

Is it also possible to fix it in more compound plots such as `corrplot`? Here is a section of what I get now:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/f/7f7787077e661fc86bf75ea752f15b3164de017f.png)

In this case, `corrplot` automates plotting a matrix of figures, so perhaps it is more difficult here… (all element plots will have different axes values…)

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 28, 2021, 11:10am UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/4 "2021-10-28T11:10:26Z")

</div>

…one possible alternative is to set `xticks` and `yticks` to `false`, I guess…

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

---

<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:** [October 28, 2021, 11:22am UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/5 "2021-10-28T11:22:28Z")

</div>

> [@BLI](#):
>
> Is it also possible to fix it in more compound plots such as `corrplot` ?

Not perfect fix but way better than default:

```julia
gr(dpi=1200)
M = rand(LinRange(1.5123,1.51234,100), 4, 4)
M[:,2] .+= 0.8sqrt.(abs.(M[:,1])) .- 0.5M[:,3] .+ 5
M[:,3] .-= 0.7M[:,1].^2 .+ 2
corrplot(M, label = ["x$i" for i=1:4], tickfontsize=1, guidefontsize=3, formatter=:plain)

```

 ![StatsPlots_corrplot_fix_ticks](https://global.discourse-cdn.com/julialang/original/3X/1/d/1d17c935f026e3241cb00835d3836e98517978bc.jpeg)

---

<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:** [October 28, 2021, 11:33am UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/6 "2021-10-28T11:33:54Z")

</div>

Actually, using the code above but saving the corrplot to a format that allows arbitrary zoom (ex: svg, pdf), the result looks OKish. Needs further plumbing.  
Better filling an issue, if there isn’t one on this yet.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 28, 2021, 1:15pm UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/7 "2021-10-28T13:15:48Z")

</div>

I’m getting quite happy with the results of your suggestions so far. One thing, though…  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/7/67aca3e4bdf405d3bb1fe7d0d25f1bfc3eeef306.png)

Is it possible to do the same trick with labels on the _ordinate_ axis as you proposed on the _abscissa_?

To do so, I need to know the variation in the “density” of the iterations in Turing… I have used the `density` plot function of `StatsPlot`, and that function hides how it computes the density. I assume it uses some sort of histogram function, but the peak of the density depends on a lot of things (number of bins, or whatever).

_Question_: Is there a way to extract the chosen `ylims` value from a plot?

---

<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:** [October 28, 2021, 1:58pm UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/8 "2021-10-28T13:58:49Z")

</div>

> [@BLI](#):
>
> Is there a way to extract the chosen `ylims` value from a plot?

This gives the plot y-bounds:

```julia
p = plot(-10:10,-100:10:100)
y0, y1 = Plots.ylims(p)

```

---

<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:** [October 28, 2021, 2:01pm UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/9 "2021-10-28T14:01:51Z")

</div>

> [@BLI](#):
>
> Is it possible to do the same trick with labels on the _ordinate_ axis as you proposed on the _abscissa_ ?

Sorry, have you tried `formatter=:plain`? _(it applies to both axes)_  
(_or_ `yformatter=:plain` _for y-axis only_)

---

<div class="post-metadata">

**Author:** ![LeePhillips](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leephillips/32/205514_2.png) [@LeePhillips](https://discourse.julialang.org/u/LeePhillips)\
**Post date:** [October 28, 2021, 2:08pm UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/10 "2021-10-28T14:08:24Z")

</div>

How about setting `rotation`?

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 29, 2021, 6:51am UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/11 "2021-10-29T06:51:50Z")

</div>

I’ve got an array plot (layout = (3,2))…

- Is it possible to extract individual plots from this layout, and operate on the individual plot?
- Is it possible to _scale_ the data series, e.g., the ordinate values `y` when I do `plot (x,y)`.

[I’m trying to plot the prior distribution and the posterior distribution in the same plot, and need to scale the ordinate values so that they have similar peak value… – it is the shift in location and width in the abscissa direction that is of interest in this comparison.]

---

<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:** [October 29, 2021, 7:35am UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/12 "2021-10-29T07:35:08Z")

</div>

> [@BLI](#):
>
> I’ve got an array plot (layout = (3,2))…
> 
> - Is it possible to extract individual plots from this layout, and operate on the individual plot?

Check this [post](https://discourse.julialang.org/t/subplots-in-plots-jl-with-individual-axis-labels/8462/4).

First do `p.spmap` to see what you’ve got. In case of `p = corrplot(...)` there are 16 subplots (4x4):

```julia
julia> p.spmap
Dict{Any, Plots.Subplot} with 16 entries:
  Symbol("##976") => Subplot{4}
  Symbol("##980") => Subplot{8}
  Symbol("##988") => Subplot{16}
  Symbol("##983") => Subplot{11}
  Symbol("##986") => Subplot{14}
  Symbol("##978") => Subplot{6}
  Symbol("##979") => Subplot{7}
  Symbol("##984") => Subplot{12}
  Symbol("##974") => Subplot{2}
 ⋮ => ⋮

```

To access the `ylims` of a given subplot, do:

```julia
julia> ylims(p.spmap[Symbol("##976")])
(1.5123100000000003, 1.51233)

```

---

<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:** [October 29, 2021, 7:38am UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/13 "2021-10-29T07:38:12Z")

</div>

> [@BLI](#):
>
> Is it possible to _scale_ the data series, e.g., the ordinate values `y` when I do `plot (x,y)` .

Could you ellaborate on this one?

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 29, 2021, 8:10am UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/14 "2021-10-29T08:10:42Z")

</div>

Suppose I have _two_ density plots with wildly different maximal `ylims` values, and I want to show them in the same plot (i.e., comparing apriori and aposteriori distributions). For such a comparison, I don’t really care about the scaling in the ordinate direction – I mainly care about how one distribution is shifted and made wider/narrower compared to the other in the abscissa direction…

Using `density` plot, the plotting algorithm produces the series to plot (`x` values, `y` values) based on “random” outcomes (e.g., from Turing) – but the user doesn’t see the `y` values.

I don’t know how `density` works. I assume some `histogram` function is used (but what `bin` size?), and then a smooth function is fitted to the histogram … I would guess.

In summary, if I want to put two density plots in the same plot, I would like to scale the `y` value of each plot so that their peak values are comparable.

Of course – if I want to compare a _posterior_ plot with a _priori_ plot, I can get around this…

- I can plot the _posterior_ plot using `density`, then use your trick of reading `ylims`.
- Then, since the `priori` distribution is known, I can use the `Distributions` package to produce the density – where I do have some control of the scaling.

Since I actually want to compare a priori distribution and a posteriori distribution, I guess I can get around the problem…

---

<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:** [October 29, 2021, 8:53am UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/15 "2021-10-29T08:53:47Z")

</div>

> [@BLI](#):
>
> Suppose I have _two_ density plots with wildly different maximal `ylims` values

That begs for two y-axes solutions using `twinx()`.  
Example:

```julia
using StatsPlots, Measures
x1 = LinRange(1.5, 1.6, 1000); y1 = rand(x1, 1000)
x2 = LinRange(1, 3, 1000); y2 = rand(x2, 1000)
density(y1, c=:red, label="left",legend=:topleft, right_margin=20mm)
density!(twinx(), y2, c=:green, label="right",legend=:topright, right_margin=20mm)

```

 ![StatsPlots_twinx_2_density_plots](https://global.discourse-cdn.com/julialang/original/3X/b/8/b8a6ade8acc4825ec0f1e306e6f769a5daf3f81a.png)

---

<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:** [October 29, 2021, 10:22am UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/16 "2021-10-29T10:22:58Z")

</div>

Actually, if the two density plots have wildly different y-scales, then the same probably happens with the x-scales. Meaning that for a nice comparison, double x- and double y-scales might be required. Plots.jl may not cope well with that use case and you may need PyPlot, for example.

_ **NB:** if you do not care about the tick labels on the x-axis, then a linear transformation of one of the variables will do._

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 29, 2021, 11:07am UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/17 "2021-10-29T11:07:07Z")

</div>

Ah… the embarrassingly simple solution 🙂

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 29, 2021, 11:07am UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/18 "2021-10-29T11:07:52Z")

</div>

I’d want them to have the same x-axis because I want to see how the posterior distribution is changed compared to the prior distribution…

---

<div class="post-metadata">

**Author:** ![trilobit](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@trilobit](https://discourse.julialang.org/u/trilobit)\
**Post date:** [October 30, 2021, 3:50pm UTC](https://discourse.julialang.org/t/statsplot-axes-labels-overwrite-each-other/70539/19 "2021-10-30T15:50:21Z")

</div>

Or you can animate the density-plots  
i.e.

```julia
using StatsPlots
mi,ma,range=5,20,200
anim = @animate for i = 1:10
    density(randn(range),leg=false, grid=false, layout=(1,2), size=(760,300))
    density!(rand(mi:ma,range),fill=(0,0.2), sp=2)
    for i = 1:4 density!(rand(mi:ma,range), grid=false, leg=false, sp=2) end
    vline!([mi,ma],sp=2,lc=:black)
    vspan!([mi,ma],sp=2,fillalpha=0.2,color=:blue)
end;gif(anim, "density estimation.gif", fps = 5)

```
