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

**URL:** <https://discourse.julialang.org/t/how-to-adjust-the-spacing-for-a-plots-title-and-a-plots-legend-using-plots-jl/104667>\
**Category:** General Usage\
**Tags:** plotting, plots, layout-plots\
**Created:** [October 6, 2023, 3:38pm UTC](https://discourse.julialang.org/t/how-to-adjust-the-spacing-for-a-plots-title-and-a-plots-legend-using-plots-jl/104667 "2023-10-06T15:38:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)\
**Post date:** [October 6, 2023, 3:38pm UTC](https://discourse.julialang.org/t/how-to-adjust-the-spacing-for-a-plots-title-and-a-plots-legend-using-plots-jl/104667/1 "2023-10-06T15:38:53Z")

</div>

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?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/a/2a8e36395f2268e080f591383c9be5367dcb44ec.png)

```julia
# 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)

```

---

<div class="post-metadata">

**Author:** ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)\
**Post date:** [October 8, 2023, 3:28pm UTC](https://discourse.julialang.org/t/how-to-adjust-the-spacing-for-a-plots-title-and-a-plots-legend-using-plots-jl/104667/2 "2023-10-08T15:28:50Z")

</div>

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`.
