From 86ca1919dba7ec1fc80a8872d6c364facd4e68a8 Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Sat, 16 Nov 2024 16:55:40 +0100 Subject: [PATCH 1/3] set offsets to 0 --- PlotsBase/ext/GRExt.jl | 8 ++++---- PlotsBase/ext/PGFPlotsXExt.jl | 1 + PlotsBase/src/plotly.jl | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/PlotsBase/ext/GRExt.jl b/PlotsBase/ext/GRExt.jl index 43fabed6f..fe54a99b0 100644 --- a/PlotsBase/ext/GRExt.jl +++ b/PlotsBase/ext/GRExt.jl @@ -979,10 +979,10 @@ function PlotsBase._update_min_padding!(sp::Subplot{GRBackend}) # Add margin given by the user padding = ( - left = Ref(2mm + sp[:left_margin]), - top = Ref(2mm + sp[:top_margin]), - right = Ref(2mm + sp[:right_margin]), - bottom = Ref(2mm + sp[:bottom_margin]), + left = Ref(sp[:left_margin]), + top = Ref(sp[:top_margin]), + right = Ref(sp[:right_margin]), + bottom = Ref(sp[:bottom_margin]), ) # Add margin for title diff --git a/PlotsBase/ext/PGFPlotsXExt.jl b/PlotsBase/ext/PGFPlotsXExt.jl index eeda1ce8c..42b03ee37 100644 --- a/PlotsBase/ext/PGFPlotsXExt.jl +++ b/PlotsBase/ext/PGFPlotsXExt.jl @@ -311,6 +311,7 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend}) "show background rectangle" => nothing, ) end + push!(the_plot.options, "tight background" => nothing) for sp ∈ plt.subplots bb2 = bbox(sp) diff --git a/PlotsBase/src/plotly.jl b/PlotsBase/src/plotly.jl index f927ace8f..5a267a077 100644 --- a/PlotsBase/src/plotly.jl +++ b/PlotsBase/src/plotly.jl @@ -373,7 +373,7 @@ function plotly_layout(plt::Plot) w, h = plt[:size] plotattributes_out[:width], plotattributes_out[:height] = w, h plotattributes_out[:paper_bgcolor] = rgba_string(plt[:background_color_outside]) - plotattributes_out[:margin] = KW(:l => 0, :b => 20, :r => 0, :t => 20) + plotattributes_out[:margin] = KW(:l => 0, :b => 0, :r => 0, :t => 0) plotattributes_out[:annotations] = KW[] From ccaff721b79cb2294aed0e80805ac3831e4e1cef Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Thu, 2 Jan 2025 20:40:32 +0100 Subject: [PATCH 2/3] set default margin to 3mm --- NEWS.md | 1 + PlotsBase/src/Commons/attrs.jl | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index edd7fc079..45e1b6ef3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +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 - 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/src/Commons/attrs.jl b/PlotsBase/src/Commons/attrs.jl index 9bc426a49..3697b1e17 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 => 1mm, + :margin => 3mm, :left_margin => :match, :top_margin => :match, :right_margin => :match, From b71fbad41a9af04ac817a426edc062ad9fd665cc Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Fri, 3 Jan 2025 01:26:30 +0100 Subject: [PATCH 3/3] 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,