permuteaxes! and related shorthands

This commit is contained in:
yha 2019-08-08 20:45:52 +03:00
parent 3e1258aa0c
commit 05fe914230
3 changed files with 89 additions and 4 deletions

View File

@ -72,6 +72,11 @@ export
xgrid!, xgrid!,
ygrid!, ygrid!,
permuteaxes!,
swapxy!,
swapxz!,
swapyz!,
xlims, xlims,
ylims, ylims,
zlims, zlims,
@ -215,6 +220,11 @@ let PlotOrSubplot = Union{Plot, Subplot}
global yflip!(plt::PlotOrSubplot, flip::Bool = true; kw...) = plot!(plt; yflip = flip, kw...) global yflip!(plt::PlotOrSubplot, flip::Bool = true; kw...) = plot!(plt; yflip = flip, kw...)
global xaxis!(plt::PlotOrSubplot, args...; kw...) = plot!(plt; xaxis = args, kw...) global xaxis!(plt::PlotOrSubplot, args...; kw...) = plot!(plt; xaxis = args, kw...)
global yaxis!(plt::PlotOrSubplot, args...; kw...) = plot!(plt; yaxis = args, kw...) global yaxis!(plt::PlotOrSubplot, args...; kw...) = plot!(plt; yaxis = args, kw...)
global permuteaxes!(plt::PlotOrSubplot, perm...) = _permuteaxes!(plt, _axis_permutation(perm))
global permuteaxes!(plt::PlotOrSubplot) = _permuteaxes!(plt, [2,1,3])
global swapxy!(plt::PlotOrSubplot) = permuteaxes!(plt,2,1,3)
global swapxz!(plt::PlotOrSubplot) = permuteaxes!(plt,3,2,1)
global swapyz!(plt::PlotOrSubplot) = permuteaxes!(plt,1,3,2)
end end

View File

@ -756,3 +756,47 @@ function axis_drawing_info(sp::Subplot)
xticks, yticks, xaxis_segs, yaxis_segs, xtick_segs, ytick_segs, xgrid_segs, ygrid_segs, xminorgrid_segs, yminorgrid_segs, xborder_segs, yborder_segs xticks, yticks, xaxis_segs, yaxis_segs, xtick_segs, ytick_segs, xgrid_segs, ygrid_segs, xminorgrid_segs, yminorgrid_segs, xborder_segs, yborder_segs
end end
# -------------------------------------------------------------------------
# Axis permutations
const axis_letters = [:x, :y, :z]
function _extend_axis_permutation!(perm::AVec{<:Integer})
while length(perm) < 3
push!(perm, findfirst(!in(perm), 1:3))
end
perm
end
_axis_permutation(tup::Tuple) = _axis_permutation([tup...])
_axis_permutation(perm::AVec{<:Integer}) = _extend_axis_permutation!(copy(perm))
function _axis_permutation(perm::AVec{<:Symbol})
p = indexin(perm, axis_letters)
any(isnothing.(p)) && error(":$(first(filter(!in(axis_letters),perm))) is not an axis letter.")
_axis_permutation(something.(p))
end
function _permutekeys!(dict, keys, perm)
values = getindex.(Ref(dict), keys)
setindex!.(Ref(dict), permute!(values, perm), keys)
dict
end
_permuteaxes!(series::Series, perm) = _permutekeys!(series, axis_letters, perm)
function _permuteaxes!(sp::Subplot, perm)
axis_keys = [:xaxis, :yaxis, :zaxis]
_permutekeys!(sp, axis_keys, perm)
for i in 1:3
sp[axis_keys[i]][:letter] = axis_letters[i]
end
for series in sp.series_list
_permuteaxes!(series, perm)
end
sp
end
function _permuteaxes!(p::Plot, perm)
for sp in p.subplots
_permuteaxes!(sp, perm)
end
p
end

View File

@ -453,3 +453,34 @@ xaxis!(args...; kw...) = plot!(; xaxis = args
yaxis!(args...; kw...) = plot!(; yaxis = args, kw...) yaxis!(args...; kw...) = plot!(; yaxis = args, kw...)
xgrid!(args...; kw...) = plot!(; xgrid = args, kw...) xgrid!(args...; kw...) = plot!(; xgrid = args, kw...)
ygrid!(args...; kw...) = plot!(; ygrid = args, kw...) ygrid!(args...; kw...) = plot!(; ygrid = args, kw...)
"""
permuteaxes!([plt], axes...)
Permute the axes of an existing plot.
Axes may be specified by letter (`:x`, `:y`, `:z`) or number (1,2,3).
`plt` defaults to the current plot. `axes` default to `(2,1,3)`.
# Examples
## Permuting axes by letters
```julia-repl
julia> x = range(0, 5π; length=100)
julia> plot(x, cos.(x), sin.(2x))
julia> permuteaxes!(:z,:x,:y)
```
## Bar plot with horizontal bars
```julia-repl
julia> bar(rand(4))
julia> permuteaxes!()
```
See also: [`swapxy!`](@ref), [`swapxz!`](@ref), [`swapyz!`](@ref)
"""
permuteaxes!(axes...) = permuteaxes!(current(), axes...)
"Swap the x and y axes of the current plot"
swapxy!() = swapxy!(current())
"Swap the x and z axes of the current plot"
swapxz!() = swapxz!(current())
"Swap the y and z axes of the current plot"
swapyz!() = swapyz!(current())