More ticks in `Plots.jl`

Sometimes Plots.jl gives me too few ticks with some backends, especially GR, which is now my favourite. MWE:

using Plots
using StatPlots
gr() # important
x = linspace(0.2291313228541989, 0.3318750511155737, 100)
plot(density(x)) # 1 or 2 ticks

I looked at the source for something like R’s pretty, and came up with this solution:

plt = plot(density(x); xticks = optimize_ticks(extrema(x)...; k_min = 10)[1])

which works, but is there anything simpler? In particular, being able to pass k_min as an argument without saving x and computing its extrema would be convenient.

edit: I forgot to mention that I wish to avoid specifying ticks manually. I just want more of them, but otherwise automated.

It actually sounds like a bug: from here it would seem that the intended tick number, if the user doesn’t provide anything, is between 5 and 8 (but I agree that ideally those variable should be possible to give as a keyword argument). If you open an issue I can look into that.

On a separate note, you shouldn’t need to import both Plots at StatPlots (at least on master, it was fixed recently) and density(x) should be sufficient to also draw the plot.

1 Like

Thanks, opened an issue:
https://github.com/JuliaPlots/Plots.jl/issues/1045

Thanks for opening the issue! Definitely something funky with the optimize_ticks function:

julia> optimize_ticks(2.29,3.3; k_min = 5, k_max = 8)
([3.0], 2.29, 3.3)

julia> optimize_ticks(2.3,3.3; k_min = 5, k_max = 8)
([2.4, 2.6, 2.8, 3.0, 3.2], 2.3, 3.3)

julia> optimize_ticks(2.29,3.3; k_min = 5, k_max = 8, strict_span = false)
([2.2, 2.4, 2.6, 2.8, 3.0, 3.2, 3.4], 2.2, 3.4000000000000004)

julia> optimize_ticks(2.3,3.3; k_min = 5, k_max = 8, strict_span = false)
([2.2, 2.4, 2.6, 2.8, 3.0, 3.2, 3.4], 2.2, 3.4000000000000004)

the strict_span = true default seems to be the issue, I’ll see if I find a reliable solution.

1 Like

Should be fixed in Plots and PlotUtils master, now tick number is between 4 and 8:

29784472-f58db5c2-8c1b-11e7-8094-7b9d3b48e151

4 Likes