Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set offsets to 0 #5027

Open
wants to merge 3 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 27 additions & 12 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)
Comment on lines 901 to 903
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jheinen can you give what the numbers returned by GR.inqtext mean in terms of the size of the displayed area?
Do they have a unit?
Further down I try to add padding for the labels and somehow I need all kinds of correction factors to make it look good, but I am not sure that it scales well when I change the size of the plot.

Copy link
Member

@jheinen jheinen Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The values are normalized device coordinates. Hopefully, the following example describes how it works:

using GR

charHeight = 0.2

selntran(0)
setcharheight(charHeight)
settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_HALF)

setlinecolorind(4)
polyline([0, 1], [0.5 - 0.5 * charHeight, 0.5 - 0.5 * charHeight])
polyline([0, 1], [0.5 + 0.5 * charHeight, 0.5 + 0.5 * charHeight])
setlinecolorind(2)
polyline([0, 1], [0.5 - 0.8 * charHeight, 0.5 - 0.8 * charHeight])
polyline([0, 1], [0.5 + 0.7 * charHeight, 0.5 + 0.7 * charHeight])

settextfontprec(232, GR.TEXT_PRECISION_OUTLINE)
text(0.5, 0.5, "Julia!")
tbx, tby = inqtext(0.5, 0.5, "Julia!")
fillrect(tbx[1], tbx[2], tby[1], tby[3])

updatews()
charheight

The lines shown correspond to the vertical alignment attributes top, cap, base, bottom.

Expand Down Expand Up @@ -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
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
1 change: 1 addition & 0 deletions PlotsBase/ext/PGFPlotsXExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion PlotsBase/src/plotly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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[]

Expand Down
Loading