diff --git a/src/backends/pgfplotsx.jl b/src/backends/pgfplotsx.jl index 29fd4e76..483e3c88 100644 --- a/src/backends/pgfplotsx.jl +++ b/src/backends/pgfplotsx.jl @@ -346,28 +346,28 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend}) ) end # add to legend? - if sp[:legend] != :none && pgfx_should_add_to_legend(series) + if sp[:legend] != :none leg_entry = if opt[:label] isa AVec get(opt[:label], i, "") elseif opt[:label] isa AbstractString if i == 1 - opt[:label] + get(opt, :label, "") else "" end else throw(ArgumentError("Malformed label. label = $(opt[:label])")) end - if leg_entry == "" - push!(segment_plot.options, "forget plot" => nothing) - continue + if leg_entry == "" || !pgfx_should_add_to_legend(series) + push!(axis.contents[end].options, "forget plot" => nothing) + else + leg_opt = PGFPlotsX.Options() + if ribbon !== nothing + pgfx_filllegend!(axis.contents[end - 3].options, opt) + end + legend = PGFPlotsX.LegendEntry(leg_opt, leg_entry, false) + push!(axis, legend) end - leg_opt = PGFPlotsX.Options() - if ribbon !== nothing - pgfx_filllegend!(axis.contents[end - 3].options, opt) - end - legend = PGFPlotsX.LegendEntry(leg_opt, leg_entry, false) - push!(axis, legend) end end # for segments # add subplot annotations @@ -691,7 +691,7 @@ function pgfx_font(fontsize, thickness_scaling = 1, font = "\\selectfont") end function pgfx_should_add_to_legend(series::Series) - series.plotattributes[:primary] && series.plotattributes[:label] != "" && + series.plotattributes[:primary] && !( series.plotattributes[:seriestype] in ( :hexbin, diff --git a/test/test_pgfplotsx.jl b/test/test_pgfplotsx.jl index 0e81237e..c88ea2d7 100644 --- a/test/test_pgfplotsx.jl +++ b/test/test_pgfplotsx.jl @@ -20,6 +20,19 @@ end @test count(x -> x isa PGFPlotsX.Plot, axis.contents) == 1 @test !haskey(axis.contents[1].options.dict, "fill") + @testset "Legends" begin + legends_plot = plot( rand(5,2), lab = ["1" ""] ) + scatter!(legends_plot, rand(5) ) + Plots._update_plot_object(legends_plot) + axis_contents = Plots.pgfx_axes(legends_plot.o)[1].contents + leg_entries = filter( x -> x isa PGFPlotsX.LegendEntry, axis_contents ) + series = filter( x -> x isa PGFPlotsX.Plot, axis_contents ) + @test length(leg_entries) == 2 + @test !haskey(series[1].options.dict, "forget plot") + @test haskey(series[2].options.dict, "forget plot") + @test !haskey(series[3].options.dict, "forget plot") + end # testset + @testset "3D docs example" begin n = 100 ts = range(0, stop = 8π, length = n) @@ -74,8 +87,18 @@ end @test marker.options["mark options"]["line width"] == 1 end # testset @testset "Plot in pieces" begin - plot(rand(100) / 3, reg = true, fill = (0, :green)) - scatter!(rand(100), markersize = 6, c = :orange) + pic = plot(rand(100) / 3, reg = true, fill = (0, :green)) + scatter!(pic, rand(100), markersize = 6, c = :orange) + Plots._update_plot_object(pic) + axis_contents = Plots.pgfx_axes(pic.o)[1].contents + leg_entries = filter( x -> x isa PGFPlotsX.LegendEntry, axis_contents ) + series = filter( x -> x isa PGFPlotsX.Plot, axis_contents ) + @test length(leg_entries) == 2 + @test length(series) == 4 + @test haskey(series[1].options.dict, "forget plot") + @test !haskey(series[2].options.dict, "forget plot") + @test haskey(series[3].options.dict, "forget plot") + @test !haskey(series[4].options.dict, "forget plot") end # testset @testset "Marker types" begin markers = filter((m -> begin