Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix minorticks #4528

Merged
merged 11 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/arg_desc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ const _arg_desc = KW(
:gridlinewidth => (Real, "Width of the grid lines (in pixels)."),
:foreground_color_minor_grid => (ColorType, "Color of minor grid lines (`:match` matches `:foreground_color_subplot`)."),
:minorgrid => (Bool, "Adds minor grid lines and ticks to the plot. Set minorticks to change number of gridlines."),
:minorticks => (Integer, "Intervals to divide the gap between major ticks into."),
:minorticks => (Integer, "Number of minor ticks between major ticks."),
:minorgridalpha => (Real, "The alpha/opacity override for the minorgrid lines."),
:minorgridstyle => (Symbol, "Style of the minor grid lines. Choose from $(_allStyles)."),
:minorgridlinewidth => (Real, "Width of the minor grid lines (in pixels)."),
Expand Down
2 changes: 1 addition & 1 deletion src/args.jl
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ const _axis_defaults = KW(
:minorgridstyle => :solid,
:minorgridlinewidth => 0.5,
:tick_direction => :in,
:minorticks => false,
:minorticks => :auto,
:minorgrid => false,
:showaxis => true,
:widen => :auto,
Expand Down
39 changes: 27 additions & 12 deletions src/axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,25 @@ _transform_ticks(ticks::AbstractArray{T}, axis) where {T<:Dates.TimeType} =
Dates.value.(ticks)
_transform_ticks(ticks::NTuple{2,Any}, axis) = (_transform_ticks(ticks[1], axis), ticks[2])

const DEFAULT_MINOR_TICKS = Ref(4) # 4 ticks -> 5 intervals

no_minor_ticks(axis) =
if (n_minor_ticks = axis[:minorticks]) === false
true # must be tested with `===` since Bool <: Integer
elseif n_minor_ticks ∈ (:none, nothing)
true
elseif (n_minor_ticks === :auto && !axis[:minorgrid])
true
else
false
end

function get_minor_ticks(sp, axis, ticks_and_labels)
axis[:minorticks] ∈ (:none, nothing, false) && !axis[:minorgrid] && return nothing
no_minor_ticks(axis) && return

n_minor_ticks = axis[:minorticks]
ticks = first(ticks_and_labels)
length(ticks) < 2 && return nothing
length(ticks) < 2 && return

amin, amax = axis_limits(sp, axis[:letter])
scale = axis[:scale]
Expand All @@ -341,14 +356,14 @@ function get_minor_ticks(sp, axis, ticks_and_labels)
ticks = [ticks[1] - first_step / ratio; ticks; ticks[end] + last_step * ratio]
end

# default to 9 intervals between major ticks for log10 scale and 5 intervals otherwise
n = if typeof(axis[:minorticks]) <: Integer && axis[:minorticks] > 1
axis[:minorticks]
else
scale === :log10 ? 9 : 5
end
n_intervals::Int =
if !(n_minor_ticks isa Bool) && n_minor_ticks isa Integer && n_minor_ticks ≥ 0
n_minor_ticks
else # `:auto` or `true`
(log_scaled && base == 10) ? Int(base - 2) : DEFAULT_MINOR_TICKS[]
end + 1

minorticks = sizehint!(eltype(ticks)[], n * sub * length(ticks))
minorticks = sizehint!(eltype(ticks)[], n_intervals * sub * length(ticks))
for i in 2:length(ticks)
lo = ticks[i - 1]
hi = ticks[i]
Expand All @@ -357,12 +372,12 @@ function get_minor_ticks(sp, axis, ticks_and_labels)
for e in 1:sub
lo_ = lo * base^(e - 1)
hi_ = lo_ * base
step = (hi_ - lo_) / n
step = (hi_ - lo_) / n_intervals
rng = (lo_ + (e > 1 ? 0 : step)):step:(hi_ - (e < sub ? 0 : step / 2))
append!(minorticks, collect(rng))
end
else
step = (hi - lo) / n
step = (hi - lo) / n_intervals
append!(minorticks, collect((lo + step):step:(hi - step / 2)))
end
end
Expand Down Expand Up @@ -570,7 +585,7 @@ function widen_factor(axis::Axis; factor = default_widen_factor[])

# automatic behavior: widen if limits aren't specified and series type is appropriate
lims = process_limits(axis[:lims], axis)
(lims isa Tuple || lims === :round) && return nothing
(lims isa Tuple || lims === :round) && return
for sp in axis.sps, series in series_list(sp)
series.plotattributes[:seriestype] in _widen_seriestypes && return factor
end
Expand Down
2 changes: 1 addition & 1 deletion src/backends/pgfplotsx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ function pgfx_axis!(opt::Options, sp::Subplot, letter)
axis[:minorgridalpha],
axis[:minorgridstyle],
),
if (mt = axis[:minorticks]) > 1 && typeof(mt) <: Integer || mt
if length(minor_ticks) > 0
"major tick length" => "0.1cm"
else
"major tick length" => "0"
Expand Down
7 changes: 4 additions & 3 deletions src/backends/pyplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1263,9 +1263,10 @@ function _before_layout_calcs(plt::Plot{PyPlotBackend})
pyaxis."grid"(false)
end

if axis[:minorticks] > 1
if !no_minor_ticks(axis) && axis[:minorticks] isa Integer
pyaxis."set_minor_locator"(
PyPlot.matplotlib.ticker.AutoMinorLocator(axis[:minorticks]),
# NOTE: AutoMinorLocator expects a number of intervals == number of ticks + 1
PyPlot.matplotlib.ticker.AutoMinorLocator(axis[:minorticks] + 1),
)
pyaxis."set_tick_params"(
which = "minor",
Expand All @@ -1276,7 +1277,7 @@ function _before_layout_calcs(plt::Plot{PyPlotBackend})
end

if axis[:minorgrid]
axis[:minorticks] > 1 || ax."minorticks_on"() # Check if ticks were already configured
no_minor_ticks(axis) || ax."minorticks_on"() # Check if ticks were already configured
pyaxis."set_tick_params"(
which = "minor",
direction = axis[:tick_direction] === :out ? "out" : "in",
Expand Down
6 changes: 5 additions & 1 deletion src/examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,8 @@ const _examples = PlotExample[
plot(
[(0, 0), (0, 0.9), (1, 0.9), (2, 1), (3, 0.9), (80, 0)],
legend = :outertopright,
minorgrid = true,
minorticks = 1,
)
plot!([(0, 0), (0, 0.9), (2, 0.9), (3, 1), (4, 0.9), (80, 0)])
plot!([(0, 0), (0, 0.9), (3, 0.9), (4, 1), (5, 0.9), (80, 0)])
Expand Down Expand Up @@ -1320,9 +1322,11 @@ _backend_skips[:plotly] = _backend_skips[:plotlyjs]

# ---------------------------------------------------------------------------------
# make and display one plot
test_examples(i::Integer; kw...) = test_examples(:gr, i; kw...)

function test_examples(
pkgname::Symbol,
i::Int;
i::Integer;
debug = false,
disp = true,
callback = nothing,
Expand Down
1 change: 1 addition & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ should_add_to_legend(series::Series) =
)

# -----------------------------------------------------------------------
Base.iterate(plt::Plot) = iterate(plt.subplots)

Base.getindex(plt::Plot, i::Union{Vector{<:Integer},Integer}) = plt.subplots[i]
Base.length(plt::Plot) = length(plt.subplots)
Expand Down
37 changes: 37 additions & 0 deletions test/test_axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,40 @@ end
pl = plot(100:100:300, hcat([1, 2, 4], [-1, -2, -4]); yformatter = :none)
@test pl[1][:yaxis][:formatter] === :none
end

@testset "minor ticks" begin
for minorticks in (:auto, :none, nothing, false, true, 0, 1, 2, 3, 4, 5)
n_minor_ticks_per_major = if minorticks isa Bool
minorticks ? Plots.DEFAULT_MINOR_TICKS[] : 0
elseif minorticks === :auto
Plots.DEFAULT_MINOR_TICKS[]
elseif minorticks === :none || minorticks isa Nothing
0
else
minorticks
end
pl = plot(1:4; minorgrid = true, minorticks)
sp = first(pl)
for axis in (:xaxis, :yaxis)
ticks = Plots.get_ticks(sp, sp[axis], update = false)
n_expected_minor_ticks = (length(first(ticks)) - 1) * n_minor_ticks_per_major
minor_ticks = Plots.get_minor_ticks(sp, sp[axis], ticks)
n_minor_ticks = if minorticks isa Bool
if minorticks
length(minor_ticks)
else
@test minor_ticks isa Nothing
0
end
elseif minorticks === :auto
length(minor_ticks)
elseif minorticks === :none || minorticks isa Nothing
@test minor_ticks isa Nothing
0
else
length(minor_ticks)
end
@test n_minor_ticks == n_expected_minor_ticks
end
end
end
2 changes: 1 addition & 1 deletion test/test_backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ end
end

@testset "Examples" begin
if Sys.islinux()
if Sys.islinux() && get(ENV, "JULIA_PKGEVAL", "false") == "false" # NOTE: for `PkgEval` timeout
callback(m, pkgname, i) = begin
pl = m.Plots.current()
save_func = (; pgfplotsx = m.Plots.pdf, unicodeplots = m.Plots.txt) # fastest `savefig` for each backend
Expand Down