diff --git a/NEWS.md b/NEWS.md index edd7fc079..3277c44f9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,7 @@ - deprecated backends `pgfplots` and `pyplot` removed - deprecated keyword `orientation` removed +- 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 43fabed6f..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) @@ -979,10 +982,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 @@ -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/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[]