Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A get API #4783

Draft
wants to merge 25 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8e2959a
basic extraction form Plot
BeastyBlacksmith Jun 23, 2023
e53e7f1
extra_kwargs for Plot
BeastyBlacksmith Jun 23, 2023
ada3455
handle magic arguments for Plot
BeastyBlacksmith Jun 23, 2023
616cf00
factor out
BeastyBlacksmith Jun 23, 2023
a29dffe
tests for axis and subplot
BeastyBlacksmith Jun 23, 2023
9625c76
implement getting from series
BeastyBlacksmith Jun 23, 2023
72dadb3
format and stub for <--
BeastyBlacksmith Jun 23, 2023
cb5dda9
add interpolation and <-- syntax for attribute access
BeastyBlacksmith Jun 26, 2023
a69160c
integrate in Plots
BeastyBlacksmith Jun 26, 2023
62c59d6
use dev versions of Recipe packages in CI
BeastyBlacksmith Jun 27, 2023
f39ae96
disable build, format
BeastyBlacksmith Jun 27, 2023
f679518
fix ci
BeastyBlacksmith Jun 27, 2023
7642c0a
dev Recipe packages on other actions
BeastyBlacksmith Jun 27, 2023
d9a8c38
Remove PyPlot from test env
BeastyBlacksmith Jun 27, 2023
cc4be02
run Plots tests without xvfb
BeastyBlacksmith Jun 27, 2023
1f4c0a5
add prefix to ubuntu runs
BeastyBlacksmith Jun 28, 2023
5414fc4
Merge branch 'master' into bbs/getattr
BeastyBlacksmith Jun 28, 2023
aa234f8
update invalidations.yml
BeastyBlacksmith Jun 28, 2023
516ad83
uodate ci.yml
BeastyBlacksmith Jun 28, 2023
c657a47
take 2
BeastyBlacksmith Jun 28, 2023
2fb98a3
invalidations
BeastyBlacksmith Jun 28, 2023
6543a42
invalidations
BeastyBlacksmith Jun 28, 2023
3a19444
invalidations
BeastyBlacksmith Jun 28, 2023
3962cb5
invalidations
BeastyBlacksmith Jun 28, 2023
1bbf717
invalidations
BeastyBlacksmith Jun 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export
BezierCurve,

plotattr,
getattr,
scalefontsize,
scalefontsizes,
resetfontsizes
Expand Down
2 changes: 1 addition & 1 deletion src/pipeline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ function _add_the_series(plt, sp, plotattributes)
throw(ArgumentError("Unsupported type for extra keyword arguments"))
end
warn_on_unsupported(plt.backend, plotattributes)
series = Series(plotattributes)
series = Series(length(plt.series_list) + 1, sp, plotattributes)
push!(plt.series_list, series)
if (z_order = plotattributes[:z_order]) === :front
push!(sp.series_list, series)
Expand Down
66 changes: 66 additions & 0 deletions src/plotattr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,69 @@ function plotattr(attrtype::Symbol, attribute::Symbol)
def == "" ? "" : ", defaults to `$def`.",
)
end

function getattr(plt::Plot, s::Symbol)
attribute = get(_keyAliases, s, s)
_getattr(plt, plt.subplots, plt.series_list, attribute)
end
function getattr(sp::Subplot, s::Symbol)
attribute = get(_keyAliases, s, s)
_getattr(sp.plt, [sp], sp.series_list, attribute)
end
function getattr(axis::Axis, s::Symbol)
attribute = get(_keyAliases, s, s)
if attribute in _axis_args
attribute = get_attr_symbol(axis[:letter], attribute)
end
_getattr(only(axis.sps).plt, axis.sps, only(axis.sps).series_list, attribute)
end

function getattr(series::Series, s::Symbol)
attribute = get(_keyAliases, s, s)
_getattr(series.subplot.plt, [series.subplot], [series], attribute)
end

function _getattr(plt::Plot, subplots::Vector{<:Subplot}, serieses::Vector{<:Series}, attribute::Symbol)
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved
if attribute ∈ _all_plot_args
return plt[attribute]
elseif attribute ∈ _all_subplot_args && attribute ∉ _magic_subplot_args
return reduce(hcat, getindex.(subplots, attribute))
elseif (attribute ∈ _all_axis_args || attribute ∈ _lettered_all_axis_args) && attribute ∉ _magic_axis_args
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved
if attribute ∈ _lettered_all_axis_args
letters = collect(String(attribute))
letter = Symbol(first(letters))
attribute = Symbol(letters[2:end]...)
axis = get_attr_symbol(letter, :axis)
reduce(hcat, getindex.(getindex.(subplots, axis), attribute))
else
axes = (:xaxis, :yaxis, :zaxis)
return map(subplots) do sp
return NamedTuple(axis => sp[axis][attribute] for axis in axes)
end
end
elseif attribute ∈ _all_series_args && attribute ∉ _magic_series_args
return reduce(hcat, map(serieses) do series
series[attribute]
end)
else
if attribute in _all_magic_args
@info "$attribute is a magic argument. These are not present in the Plot object. Please use the more specific attribute, such as `linestyle` instead of `line`."
return missing
end
extra_kwargs = Dict(
:plot =>
haskey(plt[:extra_plot_kwargs], attribute) ?
plt[:extra_plot_kwargs][attribute] : [],
:subplots => [
i => sp[:extra_kwargs][attribute] for
(i, sp) in enumerate(subplots) if haskey(sp[:extra_kwargs], attribute)
],
:series => [
series.id => series[:extra_kwargs][attribute] for series in serieses if
haskey(series[:extra_kwargs], attribute)
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved
],
)
!all(isempty, values(extra_kwargs)) && return extra_kwargs
throw(ArgumentError("Attribute not found."))
end
end
5 changes: 4 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ struct InputWrapper{T}
obj::T
end

mutable struct Series
mutable struct Series{S}
id::Int
subplot::S
plotattributes::DefaultsDict
end

Expand Down Expand Up @@ -44,6 +46,7 @@ mutable struct Subplot{T<:AbstractBackend} <: AbstractLayout
)
end


BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved
# simple wrapper around a KW so we can hold all attributes pertaining to the axis in one place
mutable struct Axis
sps::Vector{Subplot}
Expand Down
95 changes: 95 additions & 0 deletions test/test_plotattr.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using Plots, Test

tplot = plot(
repeat([1:5, 2:6], inner = 3),
layout = @layout([a b; c]),
this = :that,
line = (5, :dash),
title = ["A" "B"],
xlims = [:auto (0, Inf)],
)
@testset "Get attributes" begin
@testset "From Plot" begin
@test getattr(tplot, :size) == default(:size) == getattr(tplot, :sizes)
@test getattr(tplot, :linestyle) == permutedims(fill(:dash, 6))
@test getattr(tplot, :title) == ["A" "B" "A"]
@test getattr(tplot, :xlims) == [:auto (0, Inf) :auto] #Note: this is different from Plots.xlims.(tplot.subplots)
@test getattr(tplot, :lims) == [
(xaxis = :auto, yaxis = :auto, zaxis = :auto),
(xaxis = (0, Inf), yaxis = :auto, zaxis = :auto),
(xaxis = :auto, yaxis = :auto, zaxis = :auto),
]
@test getattr(tplot, :this) == Dict(
:series =>
[1 => :that, 2 => :that, 3 => :that, 4 => :that, 5 => :that, 6 => :that],
:subplots => Any[],
:plot => Any[],
)
@test (@test_logs (
:info,
r"line is a magic argument",
) getattr(tplot, :line)) === missing
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved
@test_throws ArgumentError getattr(tplot, :nothere)
end
@testset "From Sublot" begin
sp = tplot[2]
@test getattr(sp, :size) == default(:size) == getattr(sp, :sizes)
@test getattr(sp, :linestyle) == permutedims(fill(:dash, 2))
@test getattr(sp, :title) == "B"
@test getattr(sp, :xlims) == (0, Inf)
@test getattr(sp, :lims) == [
(xaxis = (0, Inf), yaxis = :auto, zaxis = :auto),
]
@test getattr(sp, :this) == Dict(
:series =>
[2 => :that, 5 => :that],
:subplots => Any[],
:plot => Any[],
)
@test (@test_logs (
:info,
r"line is a magic argument",
) getattr(sp, :line)) === missing
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved
@test_throws ArgumentError getattr(sp, :nothere)
end
@testset "From Axis" begin
axis = tplot[3][:yaxis]
@test getattr(axis, :size) == default(:size) == getattr(axis, :sizes)
@test getattr(axis, :linestyle) == permutedims(fill(:dash, 2))
@test getattr(axis, :title) == "A"
@test getattr(axis, :xlims) === :auto # TODO: is this expected?
@test getattr(axis, :lims) == :auto
@test getattr(axis, :this) == Dict(
:series =>
[3 => :that, 6 => :that],
:subplots => Any[],
:plot => Any[],
)
@test (@test_logs (
:info,
r"line is a magic argument",
) getattr(axis, :line)) === missing
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved
@test_throws ArgumentError getattr(axis, :nothere)
end
@testset "From Series" begin
series = tplot[1][1]
@test getattr(series, :size) == default(:size) == getattr(series, :sizes)
@test getattr(series, :linestyle) == :dash
@test getattr(series, :title) == "A"
@test getattr(series, :xlims) == :auto
@test getattr(series, :lims) == [
(xaxis = :auto, yaxis = :auto, zaxis = :auto),
]
@test getattr(series, :this) == Dict(
:series =>
[1 => :that],
:subplots => Any[],
:plot => Any[],
)
@test (@test_logs (
:info,
r"line is a magic argument",
) getattr(series, :line)) === missing
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved
@test_throws ArgumentError getattr(series, :nothere)
end
end