-
-
Notifications
You must be signed in to change notification settings - Fork 354
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
x/y-lims are not updated when fitting subplots into layout #3633
Comments
Could you please re-run (for the values and images: I've modified your example to use a fixed seed, for reproducibility) ? Ok I seem to reproduce: extrema(y1) = (2.005860349338411, 8.541465903790503)
Plots.xlims(p1) = (0.73, 10.27)
Plots.ylims(p1) = (1.809792182704848, 8.737534070424065)
extrema(y2) = (0.10905889635595356, 9.567533636029237)
Plots.xlims(p2) = (0.73, 10.27)
Plots.ylims(p2) = (1.809792182704848, 8.737534070424065) # NOK
|
Stack triggering incorrect values: ┌ Debug:
│ exception =
│ 14-element Vector{Base.StackTraces.StackFrame}:
│ macro expansion at logging.jl:341 [inlined]
│ axis_limits(sp::Plots.Subplot{Plots.GRBackend}, letter::Symbol, should_widen::Bool, consider_aspect::Bool) at axes.jl:557
│ axis_limits(sp::Plots.Subplot{Plots.GRBackend}, letter::Symbol) at axes.jl:557
│ get_sp_lims at utils.jl:420 [inlined]
│ ylims(sp::Plots.Subplot{Plots.GRBackend}) at utils.jl:435
│ ylims(plt::Plots.Plot{Plots.GRBackend}, sp_idx::Int64) at utils.jl:445
│ ylims(plt::Plots.Plot{Plots.GRBackend}) at utils.jl:445
│ top-level scope at show.jl:955
│ eval at boot.jl:360 [inlined]
│ include_string(mapexpr::typeof(identity), mod::Module, code::String, filename::String) at loading.jl:1094
│ _include(mapexpr::Function, mod::Module, _path::String) at loading.jl:1148
│ include(mod::Module, _path::String) at Base.jl:386
│ exec_options(opts::Base.JLOptions) at client.jl:285
│ _start() at client.jl:485
└ @ Plots [...]/Plots/CoJwR/src/axes.jl:557 |
You should use using Plots; gr()
using Random; Random.seed!(1234)
p = plot(layout=grid(1, 2), frame=:box)
y1 = rand(10) .* 10
plot!(sp=1, y1, aspect_ratio=:equal)
@show extrema(y1)
@show xlims(p, 1)
@show ylims(p, 1)
y2 = rand(5) .* 10
plot!(sp=2, y2, aspect_ratio=:equal)
@show extrema(y2)
@show xlims(p, 2)
@show ylims(p, 2) Output:
|
I am sorry to say that, but that does not fix my problem 🙈 you see the values for ylims(p,1)? They still don't match the actual axes limits in the picture! |
I missed that, sorry ! Let's dig down further, this might be backend dependent ... |
yes, I don't mind the margins for now ^^ |
Ok, so what is not reflected on the plots is: extrema(y1) = (2.005860349338411, 8.541465903790503)
xlims(p, 1) = (0.73, 10.27)
ylims(p, 1) = (1.809792182704848, 8.737534070424065) # <== NOK
extrema(y2) = (0.10905889635595356, 9.567533636029237)
xlims(p, 2) = (0.88, 5.12) # <== NOK
ylims(p, 2) = (-0.17469534583424495, 9.851287878219436) right ? Something is swapped somewhere in the code ... |
exactly! It seems the axis range in the final plot is determined from either of the two axis and then adjusts the other one accordingly. So that's why always one of them is correctly (x or y) |
Ok, I am sorry, maybe I was a little confusing... I need the plots to keep the aspect ratio, and with the plotting itself I am totally fine! But, what I want are just the exact values for the axis limits as they are in the final plot. |
This seems to work, triggering rendering of the image before querying the axes limits. using Plots; gr()
using Random; Random.seed!(1234)
p = plot(layout=grid(1, 2), frame=:box, margins=3Plots.mm)
y1 = rand(10) .* 10
plot!(sp=1, y1, aspect_ratio=:equal)
@show extrema(y1)
@show xlims(1)
@show ylims(1)
y2 = rand(5) .* 10
plot!(sp=2, y2, aspect_ratio=:equal)
@show extrema(y2)
@show xlims(2)
@show ylims(2)
# show(devnull, "image/png", p)
Plots.prepare_output(p)
println()
@show xlims(1)
@show ylims(1)
@show xlims(2)
@show ylims(2) Output: extrema(y1) = (2.005860349338411, 8.541465903790503)
xlims(1) = (0.73, 10.27)
ylims(1) = (1.809792182704848, 8.737534070424065)
extrema(y2) = (0.10905889635595356, 9.567533636029237)
xlims(2) = (0.88, 5.12)
ylims(2) = (-0.17469534583424495, 9.851287878219436)
xlims(1) = (0.73, 10.27)
ylims(1) = (-1.4413732345950825, 11.988699487723995)
xlims(2) = (-0.5609591226753916, 6.560959122675392)
ylims(2) = (-0.17469534583424495, 9.851287878219436) Still seems like a bug to me. |
ohhh thank you! That's indeed a good workaround! |
The root of this bug is that we get Line 601 in 4a18098
[0, 0, 0, 0] bbox, hence the ylims are not accounting for the :equal aspect ratio.
|
How did you measure that? |
Taking this example: using Plots; gr()
using Random; Random.seed!(1234)
p = plot(layout=grid(1, 2), frame=:box, margins=3Plots.mm)
y1 = rand(10) .* 10
plot!(sp=1, y1, aspect_ratio=:equal)
@show extrema(y1)
@show xlims(1)
@show ylims(1) Sorry an all zero Adding extrema(y1) = (2.005860349338411, 8.541465903790503)
(plot_ratio, plotarea(sp)) = (NaN, BBox{l,t,r,b,w,h = 0.0mm,0.0mm, 0.0mm,0.0mm, 0.0mm,0.0mm})
(plot_ratio, plotarea(sp)) = (NaN, BBox{l,t,r,b,w,h = 0.0mm,0.0mm, 0.0mm,0.0mm, 0.0mm,0.0mm})
xlims(1) = (0.73, 10.27)
(plot_ratio, plotarea(sp)) = (NaN, BBox{l,t,r,b,w,h = 0.0mm,0.0mm, 0.0mm,0.0mm, 0.0mm,0.0mm})
(plot_ratio, plotarea(sp)) = (NaN, BBox{l,t,r,b,w,h = 0.0mm,0.0mm, 0.0mm,0.0mm, 0.0mm,0.0mm})
ylims(1) = (1.809792182704848, 8.737534070424065) |
And adding this if isnan(plot_ratio)
prepare_output(sp.plt)
plot_ratio = height(plotarea(sp)) / width(plotarea(sp))
end segfaults with a |
We could also change function get_sp_lims(sp::Subplot, letter::Symbol)
!(sp[:aspect_ratio] ∈ (:none, :auto)) && prepare_output(sp.plt) # Issue #3633
axis_limits(sp, letter)
end @BeastyBlacksmith, do you think it's worth a PR ? |
I don't think that is the right way to fix this. There is some calculation in |
The problem is |
why is it a problem that it is backend dependent? |
Because then you cannot fix #3633 (comment) which is not backend dependent (no show, savefig, display, ...). |
There are quite a few functions in the pipeline before show that are backend dependent (all the ones in |
I agree that you should get the right answer independent of the backend, but in general it would be fine to get a different answer for different backends (probably not in this particular case) |
Yes it is normal to get a different answer for a different backend. However I believe that the issue exposed in the mwe #3633 (comment) should be fixed, we can't leave it like this. |
Hey!
When I fit multiple subplots with equal aspect ratios into a layout, then their axis may be extended in order to fit the layout. However, the values for xlims and ylims are not being updated. Here's a MWE:
This produces this:
and I get the following values:
You see, for example, that the y axis of subplot 1 (the left one) goes from somewhere -1 to ~12 (ignore the cutted ticks label....), but the according ylims(p[1]) gives roughly (1.8,8.7). The same is obvious for subplot 2, where the x-axis ranges from -0.5 to 6.5 roughly, but xlims(p[2]) gives (0.88,5.12).
So... how can one get the actual limits of the axis after they have been fitted into the layout?
Thanks in advance!
(I'm using Julia v1.6.1, Plots v1.18.0)
The text was updated successfully, but these errors were encountered: