From b71fbad41a9af04ac817a426edc062ad9fd665cc Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Fri, 3 Jan 2025 01:26:30 +0100 Subject: [PATCH] improve? x and y label padding --- NEWS.md | 2 +- PlotsBase/ext/GRExt.jl | 31 +++++++++++++++++++++++-------- PlotsBase/src/Commons/attrs.jl | 2 +- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/NEWS.md b/NEWS.md index 45e1b6ef3..3277c44f9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,7 +8,7 @@ - deprecated backends `pgfplots` and `pyplot` removed - deprecated keyword `orientation` removed -- default margin is 3mm and setting it to 0 should give tight margins +- setting `margin` to 0 should give tight margins now - backends are extensions now so the backend code must be explicitly loaded using `import` with the backend package, e.g. ```julia using Plots import GR # loads backend code diff --git a/PlotsBase/ext/GRExt.jl b/PlotsBase/ext/GRExt.jl index fe54a99b0..ed660e905 100644 --- a/PlotsBase/ext/GRExt.jl +++ b/PlotsBase/ext/GRExt.jl @@ -894,7 +894,10 @@ function gr_set_tickfont(sp, letter::Symbol; kw...) ) end -# size of the text with no rotation +""" +Size of the text with no rotation. +Returns `(width, height)`. +""" function gr_text_size(str) GR.savestate() GR.selntran(0) @@ -1046,21 +1049,33 @@ function PlotsBase._update_min_padding!(sp::Subplot{GRBackend}) padding[mirrored(zaxis, :right) ? :right : :left][] += 1mm + height * l * px # NOTE: why `height` here ? end else - # Add margin for x/y ticks & labels - for (ax, tc, (a, b)) ∈ - ((xaxis, xticks, (:top, :bottom)), (yaxis, yticks, (:right, :left))) + # Add margin for x ticks & labels + (ax, tc, (a, b)) = (xaxis, xticks, (:top, :bottom)) + is_xmirrored = mirrored(ax, a) if !isempty(first(tc)) - isy = ax[:letter] ≡ :y gr_set_tickfont(sp, ax) ts = gr_get_ticks_size(tc, ax[:rotation]) - l = 0.01 + (isy ? first(ts) : last(ts)) - padding[ax[:mirror] ? a : b][] += 1mm + sp_size[isy ? 1 : 2] * l * px + l = last(ts) + padding[is_xmirrored ? a : b][] += 2 * height * l * px end if (guide = ax[:guide]) != "" gr_set_font(guidefont(ax), sp) l = last(gr_text_size(guide)) - padding[mirrored(ax, a) ? a : b][] += 1mm + height * l * px # NOTE: using `height` is arbitrary + padding[is_xmirrored ? a : b][] += sp[is_xmirrored ? :top_margin : :bottom_margin] + 1.625 * height * l * px end + # Add margin for y ticks & labels + (ax, tc, (a, b)) = (yaxis, yticks, (:right, :left)) + is_ymirrored = mirrored(ax, a) + if !isempty(first(tc)) + gr_set_tickfont(sp, ax) + ts = gr_get_ticks_size(tc, ax[:rotation]) + l = first(ts) + padding[is_ymirrored ? a : b][] += 1.5 * width * l * px + end + if (guide = ax[:guide]) != "" + gr_set_font(guidefont(ax), sp) + l = last(gr_text_size(guide)) + padding[is_ymirrored ? a : b][] += sp[is_ymirrored ? :right_margin : :left_margin] + width * l * px end end if (title = gr_colorbar_title(sp)).str != "" diff --git a/PlotsBase/src/Commons/attrs.jl b/PlotsBase/src/Commons/attrs.jl index 3697b1e17..9bc426a49 100644 --- a/PlotsBase/src/Commons/attrs.jl +++ b/PlotsBase/src/Commons/attrs.jl @@ -455,7 +455,7 @@ const _subplot_defaults = KW( :projection => :none, # can also be :polar or :3d :projection_type => :auto, # can also be :ortho(graphic) or :persp(ective) :aspect_ratio => :auto, # choose from :none or :equal - :margin => 3mm, + :margin => 1mm, :left_margin => :match, :top_margin => :match, :right_margin => :match,