Skip to content

Commit

Permalink
fix annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Jul 9, 2022
1 parent 0c6b36c commit d86c69b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 76 deletions.
46 changes: 23 additions & 23 deletions src/Plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,29 +229,29 @@ include("backends/web.jl")

include("shorthands.jl")

let PlotOrSubplot = Union{Plot,Subplot}
global title!(plt::PlotOrSubplot, s::AbstractString; kw...) = plot!(plt; title = s, kw...)
global xlabel!(plt::PlotOrSubplot, s::AbstractString; kw...) = plot!(plt; xlabel = s, kw...)
global ylabel!(plt::PlotOrSubplot, s::AbstractString; kw...) = plot!(plt; ylabel = s, kw...)
global xlims!(plt::PlotOrSubplot, lims::Tuple{<:Real,<:Real}; kw...) = plot!(plt; xlims = lims, kw...)
global ylims!(plt::PlotOrSubplot, lims::Tuple{<:Real,<:Real}; kw...) = plot!(plt; ylims = lims, kw...)
global zlims!(plt::PlotOrSubplot, lims::Tuple{<:Real,<:Real}; kw...) = plot!(plt; zlims = lims, kw...)
global xlims!(plt::PlotOrSubplot, xmin::Real, xmax::Real; kw...) = plot!(plt; xlims = (xmin, xmax), kw...)
global ylims!(plt::PlotOrSubplot, ymin::Real, ymax::Real; kw...) = plot!(plt; ylims = (ymin, ymax), kw...)
global zlims!(plt::PlotOrSubplot, zmin::Real, zmax::Real; kw...) = plot!(plt; zlims = (zmin, zmax), kw...)
global xticks!(plt::PlotOrSubplot, ticks::TicksArgs; kw...) = plot!(plt; xticks = ticks, kw...)
global yticks!(plt::PlotOrSubplot, ticks::TicksArgs; kw...) = plot!(plt; yticks = ticks, kw...)
global xticks!(plt::PlotOrSubplot, ticks::AVec{<:Real}, labels::AVec{<:AbstractString}; kw...) = plot!(plt; xticks = (ticks, labels), kw...)
global yticks!(plt::PlotOrSubplot, ticks::AVec{<:Real}, labels::AVec{<:AbstractString}; kw...) = plot!(plt; yticks = (ticks, labels), kw...)
global xgrid!(plt::PlotOrSubplot, args...; kw...) = plot!(plt; xgrid = args, kw...)
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{<:Tuple}; kw...) = 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...)
end
const PlotOrSubplot = Union{Plot,Subplot}

title!(plt::PlotOrSubplot, s::AbstractString; kw...) = plot!(plt; title = s, kw...)
xlabel!(plt::PlotOrSubplot, s::AbstractString; kw...) = plot!(plt; xlabel = s, kw...)
ylabel!(plt::PlotOrSubplot, s::AbstractString; kw...) = plot!(plt; ylabel = s, kw...)
xlims!(plt::PlotOrSubplot, lims::Tuple{<:Real,<:Real}; kw...) = plot!(plt; xlims = lims, kw...)
ylims!(plt::PlotOrSubplot, lims::Tuple{<:Real,<:Real}; kw...) = plot!(plt; ylims = lims, kw...)
zlims!(plt::PlotOrSubplot, lims::Tuple{<:Real,<:Real}; kw...) = plot!(plt; zlims = lims, kw...)
xlims!(plt::PlotOrSubplot, xmin::Real, xmax::Real; kw...) = plot!(plt; xlims = (xmin, xmax), kw...)
ylims!(plt::PlotOrSubplot, ymin::Real, ymax::Real; kw...) = plot!(plt; ylims = (ymin, ymax), kw...)
zlims!(plt::PlotOrSubplot, zmin::Real, zmax::Real; kw...) = plot!(plt; zlims = (zmin, zmax), kw...)
xticks!(plt::PlotOrSubplot, ticks::TicksArgs; kw...) = plot!(plt; xticks = ticks, kw...)
yticks!(plt::PlotOrSubplot, ticks::TicksArgs; kw...) = plot!(plt; yticks = ticks, kw...)
xticks!(plt::PlotOrSubplot, ticks::AVec{<:Real}, labels::AVec{<:AbstractString}; kw...) = plot!(plt; xticks = (ticks, labels), kw...)
yticks!(plt::PlotOrSubplot, ticks::AVec{<:Real}, labels::AVec{<:AbstractString}; kw...) = plot!(plt; yticks = (ticks, labels), kw...)
xgrid!(plt::PlotOrSubplot, args...; kw...) = plot!(plt; xgrid = args, kw...)
ygrid!(plt::PlotOrSubplot, args...; kw...) = plot!(plt; ygrid = args, kw...)
annotate!(plt::PlotOrSubplot, anns...; kw...) = plot!(plt; annotations = collect(anns), kw...)
annotate!(plt::PlotOrSubplot, anns::AVec{<:Tuple}; kw...) = plot!(plt; annotations = anns, kw...)
xflip!(plt::PlotOrSubplot, flip::Bool = true; kw...) = plot!(plt; xflip = flip, kw...)
yflip!(plt::PlotOrSubplot, flip::Bool = true; kw...) = plot!(plt; yflip = flip, kw...)
xaxis!(plt::PlotOrSubplot, args...; kw...) = plot!(plt; xaxis = args, kw...)
yaxis!(plt::PlotOrSubplot, args...; kw...) = plot!(plt; yaxis = args, kw...)

# ---------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ for name in (
"backends",
)
@testset "$name" begin
gr()
gr() # reset to default backend
include("test_$name.jl")
end
end
110 changes: 58 additions & 52 deletions test/test_backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ is_ci() = get(ENV, "CI", "false") == "true"
const TEST_MODULE = Module(:PlotsTestModule)
const PLOTS_IMG_TOL = parse(
Float64,
get(ENV, "PLOTS_IMG_TOL", is_ci() ? (Sys.iswindows() ? "2e-3" : "1e-4") : "1e-5"),
get(ENV, "PLOTS_IMG_TOL", is_ci() ? "1e-4" : "1e-5"),
)

Base.eval(TEST_MODULE, quote
Expand Down Expand Up @@ -43,7 +43,7 @@ function replace_rand(ex::Expr)
for arg in ex.args
push!(expr.args, replace_rand(arg))
end
if ex.head === :call && ex.args[1] (:rand, :randn, :(Plots.fakedata))
if Meta.isexpr(ex, :call) && ex.args[1] (:rand, :randn, :(Plots.fakedata))
pushfirst!(expr.args, ex.args[1])
expr.args[2] = :rng
end
Expand Down Expand Up @@ -94,7 +94,7 @@ function image_comparison_facts(
pkg::Symbol;
skip = [], # skip these examples (int index)
only = nothing, # limit to these examples (int index)
debug = false, # print debug information?
debug = false, # print debug information ?
sigma = [1, 1], # number of pixels to "blur"
tol = 1e-2, # acceptable error (percent)
)
Expand Down Expand Up @@ -128,61 +128,67 @@ with(:pgfplotsx) do
end
=#

with(:unicodeplots) do
@test backend() == Plots.UnicodePlotsBackend()

io = IOContext(IOBuffer(), :color => true)

# lets just make sure it runs without error
p = plot(rand(10))
@test p isa Plot
@test show(io, p) isa Nothing

p = bar(randn(10))
@test p isa Plot
@test show(io, p) isa Nothing

p = plot([1, 2], [3, 4])
annotate!(p, [(1.5, 3.2, Plots.text("Test", :red, :center))])
hline!(p, [3.1])
@test p isa Plot
@test show(io, p) isa Nothing

p = plot([Dates.Date(2019, 1, 1), Dates.Date(2019, 2, 1)], [3, 4])
hline!(p, [3.1])
annotate!(p, [(Dates.Date(2019, 1, 15), 3.2, Plots.text("Test", :red, :center))])
@test p isa Plot
@test show(io, p) isa Nothing

p = plot([Dates.Date(2019, 1, 1), Dates.Date(2019, 2, 1)], [3, 4])
annotate!(p, [(Dates.Date(2019, 1, 15), 3.2, :auto)])
hline!(p, [3.1])
@test p isa Plot
@test show(io, p) isa Nothing

p = plot((plot(i) for i in 1:4)..., layout = (2, 2))
@test p isa Plot
@test show(io, p) isa Nothing
@testset "UnicodePlots" begin
with(:unicodeplots) do
@test backend() == Plots.UnicodePlotsBackend()

io = IOContext(IOBuffer(), :color => true)

# lets just make sure it runs without error
p = plot(rand(10))
@test p isa Plot
@test show(io, p) isa Nothing

p = bar(randn(10))
@test p isa Plot
@test show(io, p) isa Nothing

p = plot([1, 2], [3, 4])
annotate!(p, [(1.5, 3.2, Plots.text("Test", :red, :center))])
hline!(p, [3.1])
@test p isa Plot
@test show(io, p) isa Nothing

p = plot([Dates.Date(2019, 1, 1), Dates.Date(2019, 2, 1)], [3, 4])
hline!(p, [3.1])
annotate!(p, [(Dates.Date(2019, 1, 15), 3.2, Plots.text("Test", :red, :center))])
@test p isa Plot
@test show(io, p) isa Nothing

p = plot([Dates.Date(2019, 1, 1), Dates.Date(2019, 2, 1)], [3, 4])
annotate!(p, [(Dates.Date(2019, 1, 15), 3.2, :auto)])
hline!(p, [3.1])
@test p isa Plot
@test show(io, p) isa Nothing

p = plot((plot(i) for i in 1:4)..., layout = (2, 2))
@test p isa Plot
@test show(io, p) isa Nothing
end
end

with(:gr) do
ENV["PLOTS_TEST"] = "true"
ENV["GKSwstype"] = "100"
@test backend() == Plots.GRBackend()

@static if haskey(ENV, "APPVEYOR")
@info "Skipping GR image comparison tests on AppVeyor"
else
image_comparison_facts(:gr, tol = PLOTS_IMG_TOL, skip = Plots._backend_skips[:gr])
@testset "GR - reference images" begin
with(:gr) do
ENV["PLOTS_TEST"] = "true"
ENV["GKSwstype"] = "nul"
@test backend() == Plots.GRBackend()

@static if haskey(ENV, "APPVEYOR")
@info "Skipping GR image comparison tests on AppVeyor"
else
image_comparison_facts(:gr, tol = PLOTS_IMG_TOL, skip = Plots._backend_skips[:gr])
end
end
end

with(:plotlyjs) do
@test backend() == Plots.PlotlyJSBackend()
@testset "PlotlyJS" begin
with(:plotlyjs) do
@test backend() == Plots.PlotlyJSBackend()

p = plot(rand(10))
@test p isa Plot
@test_broken display(p) isa Nothing
p = plot(rand(10))
@test p isa Plot
@test_broken display(p) isa Nothing
end
end

@testset "Examples" begin
Expand Down
2 changes: 2 additions & 0 deletions test/test_shorthands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ end

ann = [(7, 3, "(7,3)"), (3, 7, text("hey", 14, :left, :top, :green))]
annotate!(p, ann)
show(devnull, p)
annotate!(p, ann...)
show(devnull, p)
annotate!(ann...)

xaxis!(p, true)
Expand Down

0 comments on commit d86c69b

Please sign in to comment.