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

PGFPlotsX: reworks #4262

Merged
merged 4 commits into from
Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
GR = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Measures = "442fdcdd-2543-5da2-b0f3-8c86c306513e"
Expand Down Expand Up @@ -43,6 +44,7 @@ FixedPointNumbers = "0.6 - 0.8"
GR = "0.64 - 0.66"
GeometryBasics = "0.2, 0.3.1, 0.4"
JSON = "0.21, 1"
LaTeXStrings = "1"
Latexify = "0.14 - 0.15"
Measures = "0.3"
NaNMath = "0.3, 1"
Expand Down
3 changes: 1 addition & 2 deletions src/axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function optimal_ticks_and_labels(ticks, alims, scale, formatter)
end

# return (continuous_values, discrete_values) for the ticks on this axis
function get_ticks(sp::Subplot, axis::Axis; update = true)
function get_ticks(sp::Subplot, axis::Axis; update = true, formatter = axis[:formatter])
if update || !haskey(axis.plotattributes, :optimized_ticks)
dvals = axis[:discrete_values]
ticks = _transform_ticks(axis[:ticks])
Expand All @@ -237,7 +237,6 @@ function get_ticks(sp::Subplot, axis::Axis; update = true)
cvals = axis[:continuous_values]
alims = axis_limits(sp, axis[:letter])
scale = axis[:scale]
formatter = axis[:formatter]
get_ticks(ticks, cvals, dvals, alims, scale, formatter)
end
end
Expand Down
1,046 changes: 456 additions & 590 deletions src/backends/pgfplotsx.jl

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/colorbars.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,13 @@ hascolorbar(series::Series) = colorbar_style(series) !== nothing
hascolorbar(sp::Subplot) =
sp[:colorbar] !== :none && any(hascolorbar(s) for s in series_list(sp))

function get_colorbar_ticks(sp::Subplot; update = true)
function get_colorbar_ticks(sp::Subplot; update = true, formatter = sp[:colorbar_formatter])
if update || !haskey(sp.attr, :colorbar_optimized_ticks)
ticks = _transform_ticks(sp[:colorbar_ticks])
cvals = sp[:colorbar_continuous_values]
dvals = sp[:colorbar_discrete_values]
clims = get_clims(sp)
scale = sp[:colorbar_scale]
formatter = sp[:colorbar_formatter]
sp.attr[:colorbar_optimized_ticks] =
get_ticks(ticks, cvals, dvals, clims, scale, formatter)
end
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import JSON

using VisualRegressionTests
using RecipesPipeline
using LaTeXStrings
using RecipesBase
using TestImages
using FileIO
Expand Down
4 changes: 2 additions & 2 deletions test/test_animations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ end
end

@testset "html" begin
p = plot([sin, cos], zeros(0), leg = false, xlims = (0, 2π), ylims = (-1, 1))
pl = plot([sin, cos], zeros(0), leg = false, xlims = (0, 2π), ylims = (-1, 1))
anim = Animation()
for x in range(0, stop = 2π, length = 10)
push!(p, x, Float64[sin(x), cos(x)])
push!(pl, x, Float64[sin(x), cos(x)])
frame(anim)
end

Expand Down
106 changes: 53 additions & 53 deletions test/test_axes.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@testset "Axes" begin
p = plot()
axis = p.subplots[1][:xaxis]
pl = plot()
axis = pl.subplots[1][:xaxis]
@test typeof(axis) == Plots.Axis
@test Plots.discrete_value!(axis, "HI") == (0.5, 1)
@test Plots.discrete_value!(axis, :yo) == (1.5, 2)
Expand Down Expand Up @@ -39,9 +39,9 @@ end
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
pl = plot(p1, p2)
@test xticks(pl) == yticks(pl) == zticks(pl) == [ticks1, ticks2]
@test xticks(pl[1]) == yticks(pl[1]) == zticks(pl[1]) == ticks1
end

@testset "Axis limits" begin
Expand Down Expand Up @@ -93,55 +93,55 @@ end
@test haskey(Plots._keyAliases, :xguideposition)
@test haskey(Plots._keyAliases, :x_guide_position)
@test !haskey(Plots._keyAliases, :xguide_position)
p = plot(1:2, xl = "x label")
@test p[1][:xaxis][:guide] === "x label"
p = plot(1:2, xrange = (0, 3))
@test xlims(p) === (0, 3)
p = plot(1:2, xtick = [1.25, 1.5, 1.75])
@test p[1][:xaxis][:ticks] == [1.25, 1.5, 1.75]
p = plot(1:2, xlabelfontsize = 4)
@test p[1][:xaxis][:guidefontsize] == 4
p = plot(1:2, xgα = 0.07)
@test p[1][:xaxis][:gridalpha] ≈ 0.07
p = plot(1:2, xgridls = :dashdot)
@test p[1][:xaxis][:gridstyle] === :dashdot
p = plot(1:2, xgridcolor = :red)
@test p[1][:xaxis][:foreground_color_grid] === RGBA{Float64}(1.0, 0.0, 0.0, 1.0)
p = plot(1:2, xminorgridcolor = :red)
@test p[1][:xaxis][:foreground_color_minor_grid] === RGBA{Float64}(1.0, 0.0, 0.0, 1.0)
p = plot(1:2, xgrid_lw = 0.01)
@test p[1][:xaxis][:gridlinewidth] ≈ 0.01
p = plot(1:2, xminorgrid_lw = 0.01)
@test p[1][:xaxis][:minorgridlinewidth] ≈ 0.01
p = plot(1:2, xtickor = :out)
@test p[1][:xaxis][:tick_direction] === :out
pl = plot(1:2, xl = "x label")
@test pl[1][:xaxis][:guide] === "x label"
pl = plot(1:2, xrange = (0, 3))
@test xlims(pl) === (0, 3)
pl = plot(1:2, xtick = [1.25, 1.5, 1.75])
@test pl[1][:xaxis][:ticks] == [1.25, 1.5, 1.75]
pl = plot(1:2, xlabelfontsize = 4)
@test pl[1][:xaxis][:guidefontsize] == 4
pl = plot(1:2, xgα = 0.07)
@test pl[1][:xaxis][:gridalpha] ≈ 0.07
pl = plot(1:2, xgridls = :dashdot)
@test pl[1][:xaxis][:gridstyle] === :dashdot
pl = plot(1:2, xgridcolor = :red)
@test pl[1][:xaxis][:foreground_color_grid] === RGBA{Float64}(1.0, 0.0, 0.0, 1.0)
pl = plot(1:2, xminorgridcolor = :red)
@test pl[1][:xaxis][:foreground_color_minor_grid] === RGBA{Float64}(1.0, 0.0, 0.0, 1.0)
pl = plot(1:2, xgrid_lw = 0.01)
@test pl[1][:xaxis][:gridlinewidth] ≈ 0.01
pl = plot(1:2, xminorgrid_lw = 0.01)
@test pl[1][:xaxis][:minorgridlinewidth] ≈ 0.01
pl = plot(1:2, xtickor = :out)
@test pl[1][:xaxis][:tick_direction] === :out
end

@testset "Aliases" begin
compare(p::Plot, s::Symbol, val, op) =
op(p[1][:xaxis][s], val) && op(p[1][:yaxis][s], val) && op(p[1][:zaxis][s], val)
p = plot(1:2, guide = "all labels")
@test compare(p, :guide, "all labels", ===)
p = plot(1:2, label = "test")
@test compare(p, :guide, "", ===)
p = plot(1:2, lim = (0, 3))
@test xlims(p) === ylims(p) === zlims(p) === (0, 3)
p = plot(1:2, tick = [1.25, 1.5, 1.75])
@test compare(p, :ticks, [1.25, 1.5, 1.75], ==)
p = plot(1:2, labelfontsize = 4)
@test compare(p, :guidefontsize, 4, ==)
p = plot(1:2, gα = 0.07)
@test compare(p, :gridalpha, 0.07, ≈)
p = plot(1:2, gridls = :dashdot)
@test compare(p, :gridstyle, :dashdot, ===)
p = plot(1:2, gridcolor = :red)
@test compare(p, :foreground_color_grid, RGBA{Float64}(1.0, 0.0, 0.0, 1.0), ===)
p = plot(1:2, minorgridcolor = :red)
@test compare(p, :foreground_color_minor_grid, RGBA{Float64}(1.0, 0.0, 0.0, 1.0), ===)
p = plot(1:2, grid_lw = 0.01)
@test compare(p, :gridlinewidth, 0.01, ≈)
p = plot(1:2, minorgrid_lw = 0.01)
@test compare(p, :minorgridlinewidth, 0.01, ≈)
p = plot(1:2, tickor = :out)
@test compare(p, :tick_direction, :out, ===)
compare(pl::Plot, s::Symbol, val, op) =
op(pl[1][:xaxis][s], val) && op(pl[1][:yaxis][s], val) && op(pl[1][:zaxis][s], val)
pl = plot(1:2, guide = "all labels")
@test compare(pl, :guide, "all labels", ===)
pl = plot(1:2, label = "test")
@test compare(pl, :guide, "", ===)
pl = plot(1:2, lim = (0, 3))
@test xlims(pl) === ylims(pl) === zlims(pl) === (0, 3)
pl = plot(1:2, tick = [1.25, 1.5, 1.75])
@test compare(pl, :ticks, [1.25, 1.5, 1.75], ==)
pl = plot(1:2, labelfontsize = 4)
@test compare(pl, :guidefontsize, 4, ==)
pl = plot(1:2, gα = 0.07)
@test compare(pl, :gridalpha, 0.07, ≈)
pl = plot(1:2, gridls = :dashdot)
@test compare(pl, :gridstyle, :dashdot, ===)
pl = plot(1:2, gridcolor = :red)
@test compare(pl, :foreground_color_grid, RGBA{Float64}(1.0, 0.0, 0.0, 1.0), ===)
pl = plot(1:2, minorgridcolor = :red)
@test compare(pl, :foreground_color_minor_grid, RGBA{Float64}(1.0, 0.0, 0.0, 1.0), ===)
pl = plot(1:2, grid_lw = 0.01)
@test compare(pl, :gridlinewidth, 0.01, ≈)
pl = plot(1:2, minorgrid_lw = 0.01)
@test compare(pl, :minorgridlinewidth, 0.01, ≈)
pl = plot(1:2, tickor = :out)
@test compare(pl, :tick_direction, :out, ===)
end
68 changes: 34 additions & 34 deletions test/test_backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,35 +135,35 @@ end
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
pl = plot(rand(10))
@test pl isa Plot
@test show(io, pl) isa Nothing

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

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

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

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

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

Expand All @@ -189,9 +189,9 @@ end
with(:plotlyjs) do
@test backend() == Plots.PlotlyJSBackend()

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

Expand All @@ -211,9 +211,9 @@ end
)
for be in backends
@info be
for (i, p) in Plots.test_examples(be, only = only, disp = false)
for (i, pl) in Plots.test_examples(be, only = only, disp = false)
fn = tempname() * ".png"
png(p, fn)
png(pl, fn)
@test filesize(fn) > 1_000
end
end
Expand Down
38 changes: 19 additions & 19 deletions test/test_components.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
ellipse(x, y, w, h) = Shape(w * sin.(ang) .+ x, h * cos.(ang) .+ y)
myshapes = [ellipse(x, rand(), rand(), rand()) for x in 1:4]
@test coords(myshapes) isa Tuple{Vector{Vector{S}},Vector{Vector{T}}} where {T,S}
local p
@test_nowarn p = plot(myshapes)
@test p[1][1][:seriestype] === :shape
local pl
@test_nowarn pl = plot(myshapes)
@test pl[1][1][:seriestype] === :shape
end

@testset "Misc" begin
Expand Down Expand Up @@ -171,7 +171,7 @@ end
@testset "Series Annotations" begin
square = Shape([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)])
@test_logs (:warn, "Unused SeriesAnnotations arg: triangle (Symbol)") begin
p = plot(
pl = plot(
[1, 2, 3],
series_annotations = (
["a"],
Expand All @@ -182,7 +182,7 @@ end
:triangle, # pass an incorrect argument
),
)
sa = p.series_list[1].plotattributes[:series_annotations]
sa = pl.series_list[1].plotattributes[:series_annotations]
@test only(sa.strs).str == "a"
@test sa.font.family == "courier"
@test sa.baseshape == square
Expand All @@ -201,29 +201,29 @@ end
"1/$i"
end

series_anns(p, series) = p.series_list[series].plotattributes[:series_annotations]
series_anns(pl, series) = pl.series_list[series].plotattributes[:series_annotations]
ann_strings(ann) = [s.str for s in ann.strs]
ann_pointsizes(ann) = [s.font.pointsize for s in ann.strs]

let p = plot(ones(3, 2), series_annotations = ["a" "d"; "b" "e"; "c" "f"])
ann1 = series_anns(p, 1)
let pl = plot(ones(3, 2), series_annotations = ["a" "d"; "b" "e"; "c" "f"])
ann1 = series_anns(pl, 1)
@test ann_strings(ann1) == ["a", "b", "c"]

ann2 = series_anns(p, 2)
ann2 = series_anns(pl, 2)
@test ann_strings(ann2) == ["d", "e", "f"]
end

let p = plot(ones(2, 2), series_annotations = (["a" "c"; "b" "d"], square))
ann1 = series_anns(p, 1)
let pl = plot(ones(2, 2), series_annotations = (["a" "c"; "b" "d"], square))
ann1 = series_anns(pl, 1)
@test ann_strings(ann1) == ["a", "b"]

ann2 = series_anns(p, 2)
ann2 = series_anns(pl, 2)
@test ann_strings(ann2) == ["c", "d"]

@test ann1.baseshape == ann2.baseshape == square
end

let p = plot(
let pl = plot(
ones(3, 2),
series_annotations = (
permutedims([
Expand All @@ -233,27 +233,27 @@ end
(12, 13),
),
)
ann1 = series_anns(p, 1)
ann1 = series_anns(pl, 1)
@test ann1.baseshape == square
@test ann1.scalefactor == (14, 15)
@test ann_strings(ann1) == ["x", "y", "z"]
@test ann_pointsizes(ann1) == [10, 20, 30]

ann2 = series_anns(p, 2)
ann2 = series_anns(pl, 2)
@test ann2.scalefactor == (12, 13)
@test ann_strings(ann2) == ["a", "b", "c"]
@test ann2.strs[1].font.pointsize == 42
end

@test_throws ArgumentError plot(ones(2, 2), series_annotations = [([1],) 2; 3 4])

p = plot([1, 2], annotations = (1.5, 2, text("foo", :left)))
x, y, txt = only(p.subplots[end][:annotations])
pl = plot([1, 2], annotations = (1.5, 2, text("foo", :left)))
x, y, txt = only(pl.subplots[end][:annotations])
@test (x, y) == (1.5, 2)
@test txt.str == "foo"

p = plot([1, 2], annotations = ((0.1, 0.5), :auto))
pos, txt = only(p.subplots[end][:annotations])
pl = plot([1, 2], annotations = ((0.1, 0.5), :auto))
pos, txt = only(pl.subplots[end][:annotations])
@test pos == (0.1, 0.5)
@test txt.str == "(a)"
end
Expand Down
Loading