Skip to content

Commit

Permalink
improve? x and y label padding
Browse files Browse the repository at this point in the history
  • Loading branch information
BeastyBlacksmith committed Jan 3, 2025
1 parent ccaff72 commit b71fbad
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 23 additions & 8 deletions PlotsBase/ext/GRExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 != ""
Expand Down
2 changes: 1 addition & 1 deletion PlotsBase/src/Commons/attrs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b71fbad

Please sign in to comment.