JuliaPlots / Plots.jl

Powerful convenience for Julia visualizations and data analysis
https://docs.juliaplots.org
Other
1.83k stars 354 forks source link

Consistent application of margin (GR at least) #2244

Open BioTurboNick opened 4 years ago

BioTurboNick commented 4 years ago

An issue I'm seeing with GR (haven't checked others yet) is that if I set the margins in one call to plot() and then use other plot!() calls to modify the graph, the margins are not preserved but also aren't merely reset. That is, I can correct for the effect of additional plot!() calls by increasing the margins in the earlier plot() call.

In keeping with some other issues where e.g. size of guide text and tick marks aren't being rendered inside the view area, perhaps there should be an effort to impose a consistent, stable layout system?

That is, first reserve space using margins, then reserve space needed for title, guide labels, tick marks, then draw plot in the remaining space? Subsequent calls to plot!() could require recalculating the layout, but at least the elements would all be in the viewport and not interacting in unpredictable ways.

mkborregaard commented 4 years ago

I'm wondering if we could use something like MakieLayout in Plots as well?

BeastyBlacksmith commented 4 years ago

Does it still happen on Plots1.2.1?

BioTurboNick commented 4 years ago

Does it still happen on Plots1.2.1?

Yes, it does appear to still happen.

dgleich commented 4 years ago

Yes, in fact, I ran into a related issue today. I wanted a plot with no margin at all. Here is what I did to get this to work in GR. I had to use a negative margin... (thankfully, this issue alerted me to that idea! I saw the 20mm somewhere else in the code.

using Measures, Plots
gr()
X = Plots.fakedata(100,5)

## This does work for gr without a margin
p = plot(X, framestyle=:none, legend=false,margin=-20mm)
xlims!(1,10)
ylims!(-0.5,0.5)
savefig("test-gr-5.pdf")

## This does not work :( 
gr()
p = plot(X, framestyle=:none, legend=false,margin=0mm)
xlims!(1,10)
ylims!(-0.5,0.5)
savefig("test-gr-6.pdf")

## This works for pyplot... 
pyplot()
p = plot(X, framestyle=:none, legend=false, margin=0mm)
xlims!(1,10)
ylims!(-0.5,0.5)
savefig("test-pyplot.pdf")

(@v1.4) pkg> st PyPlot Status ~/.julia/environments/v1.4/Project.toml [d330b81b] PyPlot v2.9.0

(@v1.4) pkg> st Plots Status ~/.julia/environments/v1.4/Project.toml [91a5bcdd] Plots v1.3.2

(@v1.4) pkg> st GR Status ~/.julia/environments/v1.4/Project.toml [28b8d3ca] GR v0.49.1

julia> versioninfo() Julia Version 1.4.1 Commit 381693d3df* (2020-04-14 17:20 UTC) Platform Info: OS: macOS (x86_64-apple-darwin18.7.0) CPU: Intel(R) Xeon(R) W-2140B CPU @ 3.20GHz WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-8.0.1 (ORCJIT, skylake) Environment: JULIA_EDITOR = atom -a JULIA_NUM_THREADS = 8

BeastyBlacksmith commented 4 years ago

It probably comes from here: https://github.com/JuliaPlots/Plots.jl/blob/8976fc4ae479b792289ddbfea13662d90a1de5db/src/subplots.jl#L7 which is a questionable default anyways...

dgleich commented 4 years ago

There's also this... https://github.com/JuliaPlots/Plots.jl/blob/8976fc4ae479b792289ddbfea13662d90a1de5db/src/backends/gr.jl#L807