Plots grid line at every integer or half integer

Is there a way to specify Plots.jl to have a grid line at every integer without manually listing the integers? I want grid lines based on the plot extent, without calculating the extent beforehand (since Plots.jl will do that).

For example, if I want to ensure ticks at all integers for the following plot:

plot(rand(100) .* 3 .- rand(-500:500), rand(100) .* 5; aspect_ratio = 1.0, gridlinewidth=2.0, gridalpha=1.0)

image

I can do

plot(rand(100) .* 3 .- rand(-500:500), rand(100) .* 5; aspect_ratio = 1.0, gridlinewidth=2.0, gridalpha=1.0, xticks=-1000:1000)

image
but the xticks argument has to be chosen to be large enough.

From my understanding of the code, it seems like the answer is no:

Would this be OK:

using Plots; gr()
p1 = plot(rand(100) .* 3 .- rand(-500:500), rand(100) .* 5; aspect_ratio = 1.0, gridlinewidth=2.0, gridalpha=1.0)
plot!(xticks = round(Int,xlims(p1)[1]):round(Int,xlims(p1)[2]))

The advantage being that there can be a number of series plotting into p1, and the integer range of xticks with step one is requested in the end, based on the actual plot limits.

1 Like

How about the following?

plot(rand(100) .* 3 .- rand(-500:500), rand(100) .* 5; aspect_ratio = 1.0, gridlinewidth=2.0, gridalpha=1.0, xtick=-10_000:10_1000, ytick=-10_000:10_000)

Instead of 10_000 you can use whatever is a sufficiently large integer for you to cover all your cases.