diff --git a/src/backends/pgfplotsx.jl b/src/backends/pgfplotsx.jl index 672636c6..1931343a 100644 --- a/src/backends/pgfplotsx.jl +++ b/src/backends/pgfplotsx.jl @@ -352,17 +352,6 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend}) series_index, ) end - # add series annotations - anns = series[:series_annotations] - for (xi, yi, str, fnt) in EachAnn(anns, series[:x], series[:y]) - pgfx_add_annotation!( - axis, - xi, - yi, - PlotText(str, fnt), - pgfx_thickness_scaling(series), - ) - end # add to legend? if sp[:legend] != :none leg_entry = if opt[:label] isa AVec @@ -388,15 +377,26 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend}) end end end # for segments - # add subplot annotations - for ann in sp[:annotations] + # add series annotations + anns = series[:series_annotations] + for (xi, yi, str, fnt) in EachAnn(anns, series[:x], series[:y]) pgfx_add_annotation!( axis, - locate_annotation(sp, ann...)..., - pgfx_thickness_scaling(sp), + xi, + yi, + PlotText(str, fnt), + pgfx_thickness_scaling(series), ) end end # for series + # add subplot annotations + for ann in sp[:annotations] + pgfx_add_annotation!( + axis, + locate_annotation(sp, ann...)..., + pgfx_thickness_scaling(sp), + ) + end push!(the_plot, axis) if length(plt.o.the_plot.elements) > 0 plt.o.the_plot.elements[1] = the_plot @@ -405,6 +405,7 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend}) end end # for subplots pgfx_plot.is_created = true + pgfx_plot.was_shown = false end # if return pgfx_plot end diff --git a/test/test_pgfplotsx.jl b/test/test_pgfplotsx.jl index c88ea2d7..65fe9902 100644 --- a/test/test_pgfplotsx.jl +++ b/test/test_pgfplotsx.jl @@ -245,15 +245,39 @@ end end # testset @testset "Annotations" begin y = rand(10) - plot( + pgfx_plot = plot( y, annotations = (3, y[3], Plots.text("this is \\#3", :left)), leg = false, ) + Plots._update_plot_object(pgfx_plot) + axis_content = Plots.pgfx_axes(pgfx_plot.o)[1].contents + nodes = filter(x -> !isa(x, PGFPlotsX.Plot), axis_content) + @test length(nodes) == 1 + mktempdir() do path + file_path =joinpath(path,"annotations.tex") + @test_nowarn savefig(pgfx_plot, file_path) + open(file_path) do io + lines = readlines(io) + @test count(s -> occursin("node", s), lines) == 1 + end + end annotate!([ (5, y[5], Plots.text("this is \\#5", 16, :red, :center)), (10, y[10], Plots.text("this is \\#10", :right, 20, "courier")), ]) + Plots._update_plot_object(pgfx_plot) + axis_content = Plots.pgfx_axes(pgfx_plot.o)[1].contents + nodes = filter(x -> !isa(x, PGFPlotsX.Plot), axis_content) + @test length(nodes) == 3 + mktempdir() do path + file_path =joinpath(path,"annotations.tex") + @test_nowarn savefig(pgfx_plot, file_path) + open(file_path) do io + lines = readlines(io) + @test count(s -> occursin("node", s), lines) == 3 + end + end annotation_plot = scatter!( range(2, stop = 8, length = 6), rand(6), @@ -267,9 +291,18 @@ end Plots.text("data", :green), ], ) - # mktempdir() do path - # @test_nowarn savefig(annotation_plot, path*"annotation.pdf") - # end + Plots._update_plot_object(annotation_plot) + axis_content = Plots.pgfx_axes(annotation_plot.o)[1].contents + nodes = filter(x -> !isa(x, PGFPlotsX.Plot), axis_content) + @test length(nodes) == 9 + mktempdir() do path + file_path =joinpath(path,"annotations.tex") + @test_nowarn savefig(annotation_plot, file_path) + open(file_path) do io + lines = readlines(io) + @test count(s -> occursin("node", s), lines) == 9 + end + end end # testset @testset "Ribbon" begin aa = rand(10)