diff --git a/src/axes.jl b/src/axes.jl index 1732f101..62d94e02 100644 --- a/src/axes.jl +++ b/src/axes.jl @@ -568,7 +568,8 @@ function default_should_widen(axis::Axis) return axis[:widen] end # automatic behavior: widen if limits aren't specified and series type is appropriate - (is_2tuple(axis[:lims]) || axis[:lims] == :round) && return false + lims = parse_limits(axis[:lims]) + (lims isa Tuple || lims == :round) && return false for sp in axis.sps for series in series_list(sp) if series.plotattributes[:seriestype] in _widen_seriestypes @@ -587,21 +588,15 @@ function round_limits(amin, amax, scale) amin, amax end -parse_limits(lims::Tuple{<:Real, <:Real}, letter) = lims -parse_limits(lims::Symbol, letter) = lims -parse_limits(lims::AVec, letter) = if length(lims) == 2 && all(x isa Real for x in lims) - Tuple(lims) -else - limits_err(lims, letter) -end -parse_limits(lims, letter) = limits_err(lims, letter) +parse_limits(lims::Tuple{<:Real,<:Real}) = lims +parse_limits(lims::Symbol) = lims +parse_limits(lims::AVec) = length(lims) == 2 && all(x isa Real for x in lims) ? Tuple(lims) : nothing +parse_limits(lims) = nothing -function limits_err(lims, letter) - @warn """Invalid limits for $letter axis. Limits should be a symbol, or a two-element tuple or vector of numbers. - $(letter)lims = $lims - """ - nothing -end +warn_invalid_limits(lims, letter) = @warn """ + Invalid limits for $letter axis. Limits should be a symbol, or a two-element tuple or vector of numbers. + $(letter)lims = $lims + """ # using the axis extrema and limit overrides, return the min/max value for this axis function axis_limits( @@ -613,7 +608,8 @@ function axis_limits( axis = sp[get_attr_symbol(letter, :axis)] ex = axis[:extrema] amin, amax = ex.emin, ex.emax - lims = parse_limits(axis[:lims], letter) + lims = parse_limits(axis[:lims]) + lims === nothing && warn_invalid_limits(axis[:lims], letter) has_user_lims = lims isa Tuple if has_user_lims lmin, lmax = lims diff --git a/src/utils.jl b/src/utils.jl index ae25576e..d1230684 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -370,10 +370,6 @@ ticksType(ticks::Tuple{T,S}) where {T<:Union{AVec,Tuple},S<:Union{AVec,Tuple}} = :ticks_and_labels ticksType(ticks) = :invalid -limsType(lims::Tuple{T,S}) where {T<:Real,S<:Real} = :limits -limsType(lims::Symbol) = lims == :auto ? :auto : :invalid -limsType(lims) = :invalid - # recursively merge kw-dicts, e.g. for merging extra_kwargs / extra_plot_kwargs in plotly) recursive_merge(x::AbstractDict...) = merge(recursive_merge, x...) # if values are not AbstractDicts, take the last definition (as does merge) diff --git a/test/test_axes.jl b/test/test_axes.jl index 0d8a1526..538b6a50 100644 --- a/test/test_axes.jl +++ b/test/test_axes.jl @@ -42,11 +42,21 @@ end pl = plot([1.05, 2.0, 2.95], ylims = :round) @test Plots.ylims(pl) == (1, 3) - pl = plot(1:3, xlims = (1, 5)) - @test Plots.xlims(pl) == (1, 5) + for x in (1:3, -10:10), xlims in ((1, 5), [1, 5]) + pl = plot(x; xlims) + @test Plots.xlims(pl) == (1, 5) + pl = plot(x; xlims, widen = true) + @test Plots.xlims(pl) == Plots.widen(1, 5) + end - pl = plot(1:3, xlims = (1, 5), widen = true) - @test Plots.xlims(pl) == Plots.widen(1, 5) + pl = plot(1:5, lims = :symmetric, widen = false) + @test Plots.xlims(pl) == Plots.ylims(pl) == (-5, 5) + + for xlims in (0, 0.0, false, true, plot()) + pl = plot(1:5; xlims) + @test Plots.xlims(pl) == Plots.widen(1, 5) + @test_logs (:warn, r"Invalid limits for x axis") match_mode=:any display(pl) + end end @testset "3D Axis" begin