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
This commit is contained in:
Benoit Pasquier 2021-04-23 16:52:17 +10:00 committed by GitHub
parent fbf3878ca5
commit 8cf268a390
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 1 deletions

View File

@ -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"

View File

@ -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[]

View File

@ -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