diff --git a/src/Plots.jl b/src/Plots.jl index 54d34615..d4a1ca58 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -72,6 +72,11 @@ export xgrid!, ygrid!, + permuteaxes!, + swapxy!, + swapxz!, + swapyz!, + xlims, ylims, zlims, @@ -211,10 +216,15 @@ let PlotOrSubplot = Union{Plot, Subplot} global ygrid!(plt::PlotOrSubplot, args...; kw...) = plot!(plt; ygrid = args, kw...) global annotate!(plt::PlotOrSubplot, anns...; kw...) = plot!(plt; annotation = anns, kw...) global annotate!(plt::PlotOrSubplot, anns::AVec{T}; kw...) where {T<:Tuple} = plot!(plt; annotation = anns, kw...) - global xflip!(plt::PlotOrSubplot, flip::Bool = true; kw...) = plot!(plt; xflip = 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 yaxis!(plt::PlotOrSubplot, args...; kw...) = plot!(plt; yaxis = args, kw...) + global xflip!(plt::PlotOrSubplot, flip::Bool = true; kw...) = plot!(plt; xflip = 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 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 diff --git a/src/axes.jl b/src/axes.jl index 8ef97f7e..1ad91015 100644 --- a/src/axes.jl +++ b/src/axes.jl @@ -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 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 diff --git a/src/shorthands.jl b/src/shorthands.jl index 776626a9..3d6c4379 100644 --- a/src/shorthands.jl +++ b/src/shorthands.jl @@ -453,3 +453,34 @@ xaxis!(args...; kw...) = plot!(; xaxis = args yaxis!(args...; kw...) = plot!(; yaxis = args, kw...) xgrid!(args...; kw...) = plot!(; xgrid = 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())