Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase space between subplots in Plots.jl

Tags:

julia

plots.jl

How can I increase the space between subplots in Plots.jl?

Minimal non-working example:

julia> using Plots; pyplot()
Plots.PyPlotBackend()

julia> data = [rand(100), rand(100)];
       histogram(data, layout=2, title=["Dataset A" "Dataset B"], legend=false)
       ylabel!("ylabel")

If you make the figure small enough, the y label of the second plot collides with the first plot.

like image 451
gTcV Avatar asked Mar 06 '23 17:03

gTcV


2 Answers

In the attributes part of Plots.jl documentation, there is a section called Subplot. There, you will find the keywords margin, top_margin, bottom_margin, left_margin and right_margin that might help you.

Minimal working example would be, then:

using Plots, Measures
pyplot()

data = [rand(100), rand(100)];

histogram(data, layout = 2,
          title = ["Dataset A" "Dataset B"], legend = false,
          ylabel = "ylabel", margin = 5mm)

Note the using Measures part, by the way. I hope this helps.

like image 158
Arda Aytekin Avatar answered Mar 17 '23 04:03

Arda Aytekin


Another workaround would be using the bottom_margin keyword argument holding the pyplot backend like this:

using Plots
pyplot()

x1 = rand(1:30, 20);
x2 = rand(1:30, 20);

# subplot 1
p1 = plot(
    x1,
    label="x1 value",
    title="x1 line plot",
    ylabel="x1",
    bottom_margin=50*Plots.mm,
);

# subplot 2
p2 = plot(
    x2,
    label="x2 value",
    title="x2 line plot",
    xlabel="sample",
    ylabel="x2",
);

plot(
    p1,
    p2,
    layout=grid(
        2,1,
    )
)

enter image description here

like image 30
Shayan Avatar answered Mar 17 '23 03:03

Shayan