How to adjust the spacing for a plot's title and a plot's legend using Plots.jl?

I am working with Plots.jl and I’d like to have more flexibility to adjust the spacings in the plots. I provide below a short piece of code that requires Plots.jl as an example that serves to drive the point home.

For this case, the spacing issues I’m dealing with are directly linked to the style of the plots. The frame style for the plots has to be :box and the legend has to be in the :outterbottom. If we run the piece of code provided, we’ll get the plot below. And as we see in the plot, I’d like to tackle the spacing between the title (rectangle in red) and the empty space in the bottom of the plot (rectangle in blue).

Plots.jl, as far as I know, doesn’t provide the option of a “subtitle”. For the plots I need, I’m using a two-line tile. As you can, the second line of the title sits too close to the upper part of the :box. Ideally, the spacing would adjust automatically, especially because there is space in the plot to move things around. Having a legend and a xlab leaves too much empty space.

I have tried using Plots.PlotMeasures so I could play with the margin. I’m not sure if I missed something, but changing margins didn’t do much. Do you know how I can adjust these spacings?

# Generate data
x = collect(1:100)
y = (2 .* x) .+ x .* randn(100)

# Plot data
plot(x,
    y,
    xlab = "Time steps",
    label = "My data",
    title = "This is a title \nAnd this is kind of a subtitle",
    titlelocation=:left,
    framestyle=:box,
    dpi=600,
    legend=:outerbottom,
    legend_columns=-1,
    fg_legend=:transparent)
1 Like

I found a way around the two issues. I’m not sure they’re the most clever solutions, but they work.

One is to simply add another \n in the end of the string used as title to have the space between title and plot. And the other is to use, inside the plot call, the parameter bottommargin. When you use Plots.PlotMeasures, you can set the bottommargin to negative values. For the plot above, I set it to -30px.