From 017fcb560e35298c5b3caaee2177fcebdb1e008f Mon Sep 17 00:00:00 2001 From: Pearl Li Date: Wed, 12 Jan 2022 05:52:43 -0800 Subject: [PATCH] Check if plot title already exists before adding again (#4027) * Check if plot title already exists before adding again * Add plot title tests --- src/pipeline.jl | 37 +++++++++++++++++++++++-------------- test/test_layouts.jl | 14 ++++++++++++++ 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/pipeline.jl b/src/pipeline.jl index 4698c8b9..8c22cdc5 100644 --- a/src/pipeline.jl +++ b/src/pipeline.jl @@ -277,26 +277,35 @@ end function _add_plot_title!(plt) plot_title = plt[:plot_title] + if plot_title != "" - the_layout = plt.layout - vspan = plt[:plot_titlevspan] - plt.layout = grid(2, 1, heights = (vspan, 1 - vspan)) - plt.layout.grid[1, 1] = subplot = Subplot(plt.backend, parent = plt.layout[1, 1]) - plt.layout.grid[2, 1] = the_layout - subplot.plt = plt + # make new subplot for plot title + if plt[:plot_titleindex] == 0 + the_layout = plt.layout + vspan = plt[:plot_titlevspan] + plt.layout = grid(2, 1, heights = (vspan, 1 - vspan)) + plt.layout.grid[1, 1] = subplot = Subplot(plt.backend, parent = plt.layout[1, 1]) + plt.layout.grid[2, 1] = the_layout + subplot.plt = plt + + top = plt.backend isa PyPlotBackend ? nothing : 0mm + bot = 0mm + plt[:force_minpad] = nothing, top, nothing, bot + subplot[:subplot_index] = last(plt.subplots)[:subplot_index] + 1 + plt[:plot_titleindex] = subplot[:subplot_index] + subplot[:framestyle] = :none + subplot[:margin] = 0px + push!(plt.subplots, subplot) + end + # propagate arguments plt[:plot_titleXXX] --> subplot[:titleXXX] + plot_titleindex = plt[:plot_titleindex] + subplot = plt.subplots[plot_titleindex] for sym in filter(x -> startswith(string(x), "plot_title"), keys(_plot_defaults)) subplot[Symbol(string(sym)[(length("plot_") + 1):end])] = plt[sym] end - top = plt.backend isa PyPlotBackend ? nothing : 0mm - bot = 0mm - plt[:force_minpad] = nothing, top, nothing, bot - subplot[:subplot_index] = last(plt.subplots)[:subplot_index] + 1 - plt[:plot_titleindex] = subplot[:subplot_index] - subplot[:framestyle] = :none - subplot[:margin] = 0px - push!(plt.subplots, subplot) end + return nothing end diff --git a/test/test_layouts.jl b/test/test_layouts.jl index 74daeea9..41ce0bdf 100644 --- a/test/test_layouts.jl +++ b/test/test_layouts.jl @@ -11,3 +11,17 @@ using Plots, Test @test pl[3][:yaxis][:scale] == :log10 @test pl[4][:yaxis][:scale] == :log10 end + +@testset "Plot title" begin + pl = plot(rand(4, 8), layout = 4, plot_title = "My title") + @test pl[:plot_title] == "My title" + @test pl[:plot_titleindex] == 5 + + plot!(pl) + @test pl[:plot_title] == "My title" + @test pl[:plot_titleindex] == 5 + + plot!(pl, plot_title = "My new title") + @test pl[:plot_title] == "My new title" + @test pl[:plot_titleindex] == 5 +end