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)
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)
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:
function get_ticks(ticks::Symbol, cvals::T, dvals, args...) where T
if ticks === :none
return T[], String[]
elseif !isempty(dvals)
n = length(dvals)
if ticks === :all || n < 16
return cvals, string.(dvals)
else
Δ = ceil(Int, n / 10)
rng = Δ:Δ:n
return cvals[rng], string.(dvals[rng])
end
else
return optimal_ticks_and_labels(nothing, args...)
end
end
if ticks === nothing
scaled_ticks = optimize_ticks(
sf(amin),
sf(amax);
k_min = 4, # minimum number of ticks
k_max = 8, # maximum number of ticks
)[1]
elseif typeof(ticks) <: Int
scaled_ticks, viewmin, viewmax = optimize_ticks(
sf(amin),
sf(amax);
k_min = ticks, # minimum number of ticks
k_max = ticks, # maximum number of ticks
k_ideal = ticks,
# `strict_span = false` rewards cases where the span of the
# chosen ticks is not too much bigger than amin - amax:
strict_span = false,
)
# axis[:lims] = map(RecipesPipeline.inverse_scale_func(scale), (viewmin, viewmax))
else
This file has been truncated. show original
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.
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.