diff --git a/Project.toml b/Project.toml index a23555b5..30da8343 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Plots" uuid = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" author = ["Tom Breloff (@tbreloff)"] -version = "1.0.0" +version = "1.0.3" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" @@ -53,12 +53,12 @@ julia = "1" FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549" GeometryTypes = "4d00f742-c7ba-57c2-abde-4428a4b178cb" Gtk = "4c0ca9eb-093a-5379-98c5-f87ac0bbbf44" +HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f" ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1" Images = "916415d5-f1e6-5110-898d-aaa5f9f070e0" LibGit2 = "76f85450-5226-5b5a-8eaa-529ad045b433" OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" PGFPlotsX = "8314cec4-20b6-5062-9cdb-752b83310925" -HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f" RDatasets = "ce6b1742-4840-55fa-b093-852dadbb1d8b" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" diff --git a/src/Plots.jl b/src/Plots.jl index 152f0f24..5c960e19 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -1,5 +1,9 @@ module Plots +if isdefined(Base, :Experimental) && isdefined(Base.Experimental, Symbol("@optlevel")) + @eval Base.Experimental.@optlevel 1 +end + const _current_plots_version = VersionNumber(split(first(filter(line -> occursin("version", line), readlines(normpath(@__DIR__, "..", "Project.toml")))), "\"")[2]) using Reexport diff --git a/src/args.jl b/src/args.jl index e4f137af..baa2a76c 100644 --- a/src/args.jl +++ b/src/args.jl @@ -1015,7 +1015,7 @@ function preprocessArgs!(plotattributes::AKW) end # handle axes args for k in _axis_args - if haskey(plotattributes, k) + if haskey(plotattributes, k) && k !== :link v = plotattributes[k] for letter in (:x, :y, :z) lk = Symbol(letter, k) diff --git a/src/backends/pgfplotsx.jl b/src/backends/pgfplotsx.jl index 672636c6..97863344 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 @@ -900,6 +901,15 @@ function pgfx_sanitize_string(s::AbstractString) s = replace(s, r"\\?\%" => "\\%") s = replace(s, r"\\?\_" => "\\_") s = replace(s, r"\\?\&" => "\\&") + s = replace(s, r"\\?\{" => "\\{") + s = replace(s, r"\\?\}" => "\\}") +end +@require LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f" begin + using LaTeXStrings + function pgfx_sanitize_string(s::LaTeXString) + s = replace(s, r"\\?\#" => "\\#") + s = replace(s, r"\\?\%" => "\\%") + end end function pgfx_sanitize_plot!(plt) for (key, value) in plt.attr diff --git a/src/examples.jl b/src/examples.jl index ff55b586..37c9e19b 100644 --- a/src/examples.jl +++ b/src/examples.jl @@ -934,6 +934,18 @@ const _examples = PlotExample[ end, ], ), + PlotExample( + "Linked axes", + "", + [ + quote + begin + x = -5:0.1:5 + plot(plot(x, x->x^2), plot(x, x->sin(x)), layout = 2, link = :y) + end + end, + ], + ), ] # Some constants for PlotDocs and PlotReferenceImages diff --git a/src/series.jl b/src/series.jl index 9a27c96b..7cd0a3f0 100644 --- a/src/series.jl +++ b/src/series.jl @@ -216,7 +216,10 @@ function _apply_type_recipe(plotattributes, v::AbstractArray, letter) end # special handling for Surface... need to properly unwrap and re-wrap -_apply_type_recipe(plotattributes, v::Surface{<:AMat{<:DataPoint}}) = v +_apply_type_recipe( + plotattributes, + v::Surface{<:AMat{<:Union{AbstractFloat, Integer, AbstractString, Missing}}}, +) = v function _apply_type_recipe(plotattributes, v::Surface) ret = _apply_type_recipe(plotattributes, v.surf) if typeof(ret) <: Formatted @@ -226,9 +229,13 @@ function _apply_type_recipe(plotattributes, v::Surface) end end -# don't do anything for datapoints or nothing -_apply_type_recipe(plotattributes, v::AbstractArray{<:DataPoint}, letter) = v +# don't do anything vectors of datapoints and for nothing _apply_type_recipe(plotattributes, v::Nothing, letter) = v +_apply_type_recipe( + plotattributes, + v::AbstractArray{<:Union{AbstractFloat, Integer, AbstractString, Missing}}, + letter, +) = v # axis args before type recipes should still be mapped to all axes function _preprocess_axis_args!(plotattributes) diff --git a/src/shorthands.jl b/src/shorthands.jl index 81d290c7..230806a4 100644 --- a/src/shorthands.jl +++ b/src/shorthands.jl @@ -438,7 +438,7 @@ julia> plot(1:10) julia> annotate!([(7,3,"(7,3)"),(3,7,text("hey", 14, :left, :top, :green))]) ``` """ -annotate!(anns...; kw...) = plot!(; annotation = anns, kw...) +annotate!(anns...; kw...) = plot!(; annotation = collect(anns), kw...) annotate!(anns::AVec{T}; kw...) where {T<:Tuple} = plot!(; annotation = anns, kw...) "Flip the current plots' x axis" 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)