From 8cf268a3903f847ef3071019b3e77b99240ba9c4 Mon Sep 17 00:00:00 2001 From: Benoit Pasquier <4486578+briochemc@users.noreply.github.com> Date: Fri, 23 Apr 2021 16:52:17 +1000 Subject: [PATCH] Add ticks getter functions (#3435) * Add ticks getter functions * Add tick-getter functions for Plot objects * Fix boiler-plate code in get_ticks * Bump minor version + add tests * Remove kwargs and add subplot test * Add docstrings * bump minor version --- Project.toml | 2 +- src/axes.jl | 60 +++++++++++++++++++++++++++++++++++++++++++++++ test/test_axes.jl | 10 ++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index c0f49d23..b5e5a626 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Plots" uuid = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" author = ["Tom Breloff (@tbreloff)"] -version = "1.12.0" +version = "1.13.0" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" diff --git a/src/axes.jl b/src/axes.jl index 2f6c4beb..038616e2 100644 --- a/src/axes.jl +++ b/src/axes.jl @@ -235,6 +235,66 @@ function get_ticks(sp::Subplot, axis::Axis; update = true) return axis.plotattributes[:optimized_ticks] end +# Ticks getter functions +for l in (:x, :y, :z) + axis = string(l, "-axis") # "x-axis" + ticks = string(l, "ticks") # "xticks" + f = Symbol(ticks) # :xticks + @eval begin + """ + $($f)(p::Plot) + + returns a vector of the $($axis) ticks of the subplots of `p`. + + Example use: + + ```jldoctest + julia> p = plot(1:5, $($ticks)=[1,2]) + + julia> $($f)(p) + 1-element Vector{Tuple{Vector{Float64}, Vector{String}}}: + ([1.0, 2.0], ["1", "2"]) + ``` + + If `p` consists of a single subplot, you might want to grab + only the first element, via + + ```jldoctest + julia> $($f)(p)[1] + ([1.0, 2.0], ["1", "2"]) + ``` + + or you can call $($f) on the first (only) subplot of `p` via + + ```jldoctest + julia> $($f)(p[1]) + ([1.0, 2.0], ["1", "2"]) + ``` + """ + $f(p::Plot) = get_ticks(p, $(Meta.quot(l))) + """ + $($f)(sp::Subplot) + + returns the $($axis) ticks of the subplot `sp`. + + Note that the ticks are returned as tuples of values and labels: + + ```jldoctest + julia> sp = plot(1:5, $($ticks)=[1,2]).subplots[1] + Subplot{1} + + julia> $($f)(sp) + ([1.0, 2.0], ["1", "2"]) + ``` + """ + $f(sp::Subplot) = get_ticks(sp, $(Meta.quot(l))) + export $f + end +end +# get_ticks from axis symbol :x, :y, or :z +get_ticks(sp::Subplot, s::Symbol) = get_ticks(sp, sp[Symbol(s, :axis)]) +get_ticks(p::Plot, s::Symbol) = [get_ticks(sp, s) for sp in p.subplots] + function get_ticks(ticks::Symbol, cvals::T, dvals, args...) where T if ticks === :none return T[], String[] diff --git a/test/test_axes.jl b/test/test_axes.jl index db2cb8cd..d981d21a 100644 --- a/test/test_axes.jl +++ b/test/test_axes.jl @@ -21,3 +21,13 @@ end # testset @test Plots.get_ticks(p2[1], p2[1][:xaxis])[2] == string.('C':3:'Z') @test Plots.get_ticks(p3[1], p3[1][:xaxis])[2] == string.('A':'Z') end + +@testset "Ticks getter functions" begin + ticks1 = ([1,2,3], ("a","b","c")) + ticks2 = ([4,5], ("e","f")) + p1 = plot(1:5, 1:5, 1:5, xticks=ticks1, yticks=ticks1, zticks=ticks1) + p2 = plot(1:5, 1:5, 1:5, xticks=ticks2, yticks=ticks2, zticks=ticks2) + p = plot(p1, p2) + @test xticks(p) == yticks(p) == zticks(p) == [ticks1, ticks2] + @test xticks(p[1]) == yticks(p[1]) == zticks(p[1]) == ticks1 +end