From bf85afe9d6b67923cc000acaa1bdb499a87949b1 Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Thu, 13 Aug 2020 11:31:17 +0200 Subject: [PATCH] unite titlefonts (#2363) * change default `plot_title` * add fontkwargs for colorbar_title, plot_title and window_title * add utility functions * adjust default colorbar title fontsize * remove window-title attributes and edit description * add match_map entries * remove plot_fontfamily add match for colorbar_fontfamily * add process_any_label * add tests * add magic arguments * adjust tests * fix subplot args --- src/arg_desc.jl | 2 +- src/args.jl | 36 ++++++++++++++++++++++++++++-------- src/components.jl | 3 +++ src/utils.jl | 18 ++++++++++++++++++ test/test_pgfplotsx.jl | 19 +++++++++++++++++++ 5 files changed, 69 insertions(+), 9 deletions(-) diff --git a/src/arg_desc.jl b/src/arg_desc.jl index 4c81768f..9adad50b 100644 --- a/src/arg_desc.jl +++ b/src/arg_desc.jl @@ -59,7 +59,7 @@ const _arg_desc = KW( :foreground_color => "Color Type. Base color for all foregrounds.", :size => "NTuple{2,Int}. (width_px, height_px) of the whole Plot", :pos => "NTuple{2,Int}. (left_px, top_px) position of the GUI window (note: currently unimplemented)", -:window_title => "String. Title of the window.", +:window_title => "String. Title of the standalone gui-window.", :show => "Bool. Should this command open/refresh a GUI/display? This allows displaying in scripts or functions without explicitly calling `display`", :layout => "Integer (number of subplots), NTuple{2,Integer} (grid dimensions), AbstractLayout (for example `grid(2,2)`), or the return from the `@layout` macro. This builds the layout of subplots.", :link => "Symbol. How/whether to link axis limits between subplots. Values: `:none`, `:x` (x axes are linked by columns), `:y` (y axes are linked by rows), `:both` (x and y are linked), `:all` (every subplot is linked together regardless of layout position).", diff --git a/src/args.jl b/src/args.jl index e2051abb..58f4464e 100644 --- a/src/args.jl +++ b/src/args.jl @@ -294,6 +294,13 @@ const _series_defaults = KW( const _plot_defaults = KW( :plot_title => "", + :plot_titlefontsize => 16, + :plot_title_location => :center, # also :left or :right + :plot_titlefontfamily => :match, + :plot_titlefonthalign => :hcenter, + :plot_titlefontvalign => :vcenter, + :plot_titlefontrotation => 0.0, + :plot_titlefontcolor => :match, :background_color => colorant"white", # default for all backgrounds, :background_color_outside => :match, # background outside grid, :foreground_color => :auto, # default for all foregrounds, and title color, @@ -360,10 +367,18 @@ const _subplot_defaults = KW( :right_margin => :match, :bottom_margin => :match, :subplot_index => -1, - :colorbar_title => "", - :framestyle => :axes, - :camera => (30,30), - :extra_kwargs => Dict() + :colorbar_title => "", + :colorbar_titlefontsize => 10, + :colorbar_title_location => :center, # also :left or :right + :colorbar_fontfamily => :match, + :colorbar_titlefontfamily => :match, + :colorbar_titlefonthalign => :hcenter, + :colorbar_titlefontvalign => :vcenter, + :colorbar_titlefontrotation => 0.0, + :colorbar_titlefontcolor => :match, + :framestyle => :axes, + :camera => (30,30), + :extra_kwargs => Dict() ) const _axis_defaults = KW( @@ -468,7 +483,7 @@ const _subplot_args = sort(union(collect(keys(_subplot_defaults)))) const _plot_args = sort(union(collect(keys(_plot_defaults)))) const _magic_axis_args = [:axis, :tickfont, :guidefont, :grid, :minorgrid] -const _magic_subplot_args = [:titlefont, :legendfont, :legendtitlefont, ] +const _magic_subplot_args = [:titlefont, :legendfont, :legendtitlefont, :plot_titlefont, :colorbar_titlefont] const _magic_series_args = [:line, :marker, :fill] const _all_axis_args = sort(union([_axis_args; _magic_axis_args])) @@ -1048,7 +1063,7 @@ function RecipesPipeline.preprocess_attributes!(plotattributes::AKW) end # fonts - for fontname in (:titlefont, :legendfont, :legendtitlefont) + for fontname in (:titlefont, :legendfont, :legendtitlefont, :plot_titlefont, :colorbar_titlefont) args = RecipesPipeline.pop_kw!(plotattributes, fontname, ()) for arg in wraptuple(args) processFontArg!(plotattributes, fontname, arg) @@ -1285,11 +1300,16 @@ const _match_map = KW( :right_margin => :margin, :bottom_margin => :margin, :titlefontfamily => :fontfamily_subplot, - :legendfontfamily => :fontfamily_subplot, - :legendtitlefontfamily => :fontfamily_subplot, :titlefontcolor => :foreground_color_subplot, + :legendfontfamily => :fontfamily_subplot, :legendfontcolor => :foreground_color_subplot, + :legendtitlefontfamily => :fontfamily_subplot, :legendtitlefontcolor => :foreground_color_subplot, + :colorbar_fontfamily => :fontfamily_subplot, + :colorbar_titlefontfamily => :fontfamily_subplot, + :colorbar_titlefontcolor => :foreground_color_subplot, + :plot_titlefontfamily => :fontfamily, + :plot_titlefontcolor => :foreground_color, :tickfontcolor => :foreground_color_text, :guidefontcolor => :foreground_color_guide, ) diff --git a/src/components.jl b/src/components.jl index 89bca909..71894343 100644 --- a/src/components.jl +++ b/src/components.jl @@ -640,6 +640,9 @@ function process_annotation(sp::Subplot, positions::Union{AVec{Symbol},Symbol}, anns end +function process_any_label(lab, font=Font()) + lab isa Tuple ? text(lab...) : text( lab, font ) +end # Give each annotation coordinates based on specified position function locate_annotation(sp::Subplot, pos::Symbol, lab::PlotText) position_multiplier = Dict{Symbol, Tuple{Float64,Float64}}( diff --git a/src/utils.jl b/src/utils.jl index 0fd11167..11c7b210 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1004,6 +1004,24 @@ ignorenan_extrema(plt::Plot) = (xmin(plt), xmax(plt)) # --------------------------------------------------------------- # get fonts from objects: +plottitlefont(p::Plot) = font( + p[:plot_titlefontfamily], + p[:plot_titlefontsize], + p[:plot_titlefontvalign], + p[:plot_titlefonthalign], + p[:plot_titlefontrotation], + p[:plot_titlefontcolor], +) + +colorbartitlefont(sp::Subplot) = font( + sp[:colorbar_titlefontfamily], + sp[:colorbar_titlefontsize], + sp[:colorbar_titlefontvalign], + sp[:colorbar_titlefonthalign], + sp[:colorbar_titlefontrotation], + sp[:colorbar_titlefontcolor], +) + titlefont(sp::Subplot) = font( sp[:titlefontfamily], sp[:titlefontsize], diff --git a/test/test_pgfplotsx.jl b/test/test_pgfplotsx.jl index ed408ad5..2e640ead 100644 --- a/test/test_pgfplotsx.jl +++ b/test/test_pgfplotsx.jl @@ -357,3 +357,22 @@ end # testset axes = Plots.pgfx_axes(pl.o) @test filter(x->x isa String, axes[1].contents)[1] == raw"\node at (0,0.5) {\huge hi};" end # testset + +@testset "Titlefonts" begin + pl = plot(1:5, title = "Test me", titlefont = (2, :left)) + @test pl[1][:title] == "Test me" + @test pl[1][:titlefontsize] == 2 + @test pl[1][:titlefonthalign] == :left + Plots._update_plot_object(pl) + ax_opt = Plots.pgfx_axes(pl.o)[1].options + @test ax_opt["title"] == "Test me" + @test(haskey(ax_opt.dict, "title style")) isa Test.Pass + pl = plot(1:5, plot_title = "Test me", plot_titlefont = (2, :left)) + @test pl[:plot_title] == "Test me" + @test pl[:plot_titlefontsize] == 2 + @test pl[:plot_titlefonthalign] == :left + pl = heatmap(rand(3,3), colorbar_title = "Test me", colorbar_titlefont = (12, :right)) + @test pl[1][:colorbar_title] == "Test me" + @test pl[1][:colorbar_titlefontsize] == 12 + @test pl[1][:colorbar_titlefonthalign] == :right +end # testset