Number of digits in xticks for Plots.jl

Hi all-

I have a crowding issue on the scale of my x-axis because too many digits are displayed. I tried to round the values I pass to xticks, but that did not help. Is there any way to control the number of digits that are display?

2 Likes

What command are you currently using to plot? xticks can take a range with a step, like this: 1:2:10 which will return ticks at these locations:

julia> collect(1:2:10)
5-element Array{Int64,1}:
 1
 3
 5
 7
 9

I am passing a step range like this: round.(range(…),digits =2). I am having trouble reproducing the problem with a minimum working example. It must depend on some combination of factors that I do not fully understand.

This works:

using Plots,Distributions
Parms = [[100.0,15.0],[0.0,.005],[10.0,5.0],[10,.01]]
p = plot(layout=4)
for (i,parms) in enumerate(Parms)
    x = rand(Normal(parms...),100)
    y = rand(Normal(parms...),100)
    lb = minimum(x); ub = maximum(x)
    ticks = round.(range(lb,stop=ub,length = 4),digits=2)
    scatter!(p,x,y,subplot=i,xticks=ticks)
end
display(p)

But as you can see from my more complicated example, sometimes it does not work. I was able to work around this particular problem by changing the size of the plot. I will repost if I can reproduce with something that I can share.

1 Like

I just want to clarify that my example is basically the same except that I am using simulated data.

Ah, are you talking about the example in the upper right? You can reduce the fontsize for the axis labels (using e.g. xtickfont = font(20, "Courier")). I’m not quite sure, but I think there was an option to rotate the tick labeling too, but I can’t recall how its called right now.

2 Likes

The upper left and lower right subplots are problematic because they should have two decimals. The upper right is overcrowded, but conforms to the intended configuration. I guess this isn’t a general problem, but hopefully I can identify the parameters that produce it.

Thanks for the ideas nonetheless.

You can convert the ticks to strings with the format you want using Printf and then use the strings as tick labels. For example:

using Plots, Printf
x = rand(10); y = rand(10);
ticks = collect(0:0.2:1)
ticklabels = [ @sprintf("%5.1f",x) for x in ticks ]
plot(x,y)
plot!(xticks=(ticks,ticklabels))

which will give you the number of decimal places defined by printf

3 Likes