Layouts of a plot and of the same in a subplot

Continuing the discussion from Plot size from subplots sizes !
The discussion was about the layout changes illustrated below.
The first plot was good-looking, but the combination in subplots was not.
A useful workaround was to add a 5mm margin in the second test to make it more readable.
However, when combining other kinds of plots in a subplot, the problem pops up again and again.
The problem boils down to the fact that even when providing the correct space for the subplots,
the layout of the subplots are strongly modified, not only the sizes, margins and other elements are different, but even the aspect ratio of the frame is modified.
I included a 3rd test showing that increasing the width of the initial plot leads to a similar distortion. This may give a clue to what is happening.

Would you know any reason for this behaviour?
Would you know a way to avoid it in a reproducible way?



Test 1: a 400x300 plot

p = testplot()


Test 2: a 800x300 plot with two subplots as above (400x300)

p = testplot()
plot(p,p, layout=grid(1,2, widths=(400/800, 400/800)), size=(800,300))


Test 3: similar behaviour when plotting same plot twice larger (compare to test 1)

plot(p, size=(800,300))

Source code of testplot()

using Plots

function testplot()
    Di = 1:9
    Ei = [215, 125, 89, 69, 56, 47, 41, 36, 32]
    Oi = [216, 163, 88, 74, 61, 40, 25, 26, 22]
    markColor = [:green,:red,:green,:green,:green,:green,:red,:red,:red]
    title = "This is a test\n N=175, chi²=26  ** Poor fit"
    p = plot(xtickfontsize=8,ytickfontsize=8,xguidefontsize=8,yguidefontsize=8, titlefont=font(10))
    plot!(size=(400,300), background_color="ivory", framestyle = :box)
    plot!(title=title, xlabel="digit", ylabel="count")
    plot!(xticks=Di, ylim=(0,Inf))
    plot!(Di, Ei, ribbon=1*sqrt.(Ei), label="1σ CI", fillcolor="gray65")
    plot!(Di, Ei, ribbon=2*sqrt.(Ei), label="2σ CI", fillcolor="gray85")
    scatter!(Di, Oi, label="data", markercolor=markColor)
1 Like

Just a simplified example:

using Plots
plot(rand(10),size=(400,300),title="This is a test",xlabel="Test",ylabel="Test")


plot(rand(10),size=(800,300),title="This is a test",xlabel="Test",ylabel="Test")

Looks like a bug or an inconsistency in how the layout depends on the width.
I cant see any sense there.