# Some questions about \`Plots\`

**URL:** https://discourse.julialang.org/t/some-questions-about-plots/116649
**Category:** Visualization
**Tags:** question, plots
**Created:** [July 5, 2024, 9:14am UTC](https://discourse.julialang.org/t/some-questions-about-plots/116649 "2024-07-05T09:14:17Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [July 5, 2024, 9:14am UTC](https://discourse.julialang.org/t/some-questions-about-plots/116649/1 "2024-07-05T09:14:17Z")

</div>

1. How to make the title closer to the figure?
2. How to make the xlabel/ylabel and the tick numbers closer to the axis?
3. In some cases, if I set the size of the combined figure, some of xlables or ylabels near to the border will be covered. Why? How to fix this?
4. ~~How to define and show a customized legend?~~

```julia
using Plots

PLT = []
for i in 1:4
    plt = plot(
        rand(100);
        title="test", 
        xlabel = "Xlabel",
        ylabel = "Ylabel",
        size=(300, 300),
    )
    push!(PLT, plt)
end
plot(PLT..., layout=(2,2); size=(600, 600))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/4/b475056d36f6580fbf52603ca5fe674c1ee83343.png)

![image](https://global.discourse-cdn.com/julialang/original/3X/6/5/657c8b1ffe7e774fea4e87738e0c7295c6a36989.png)

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [July 5, 2024, 11:50am UTC](https://discourse.julialang.org/t/some-questions-about-plots/116649/2 "2024-07-05T11:50:10Z")

</div>

> [@WuSiren](#):
>
> - In some cases, if I set the size of the combined figure, some of xlables or ylabels near to the border will be covered. Why? How to fix this?
> - How to define and show a customized legend?

The first is with the `margin` keyword. The second I guess you are looking for the `label` keyword?

```julia-repl
julia> plot([rand(10) rand(10)]; 
           layout=(1,2), 
           size=(600,600), 
           label=["abc" "def"],
           xlabel="my x label",
           ylabel="my y label",
           margin=1.0Plots.Measures.cm
       )

```

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

---

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [July 5, 2024, 11:58am UTC](https://discourse.julialang.org/t/some-questions-about-plots/116649/3 "2024-07-05T11:58:57Z")

</div>

> [@lmiq](#):
>
> `margin=1.0Plots.Measures.cm`

Thanks, Prof. @lmiq ! The margin keyword is indeed useful, but it make the subfigures scale too small, and the title and the xlabel/ylabel become more far away from the axises.  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/c/cc4abd7be50eaf7ddf52a69836ef6f78fae240c9.png)

I really need a method to making the title and xlabel/ylabel more close to the axis, but `Plots` doesn’t seem to support.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [July 5, 2024, 12:09pm UTC](https://discourse.julialang.org/t/some-questions-about-plots/116649/4 "2024-07-05T12:09:22Z")

</div>

You can use `leftmargin`, `bottommargin`, and `rightmargin` independently, and the result might be better:

```julia-repl
julia> plot(PLT..., layout=(2,2); size=(600, 600),
           leftmargin=1.0Plots.Measures.cm,
           rightmargin=1.0Plots.Measures.cm,
           bottommargin=1.0Plots.Measures.cm,
       )

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/0/f03dca30d2f665e46a1fcea43b612a17e0e4aeb5.png)

The distances of the axis labels to the plots I don’t know how to change, but I don´t see they can be much closer to the axis there without overlapping with the numbers.

I usually play with some combination of margins, size, and the `scalefontsizes` function, to get a reasonable result:

```julia-repl
julia> scalefontsizes(); scalefontsizes(1.5)

julia> plot(PLT..., layout=(2,2); size=(800, 800),
           leftmargin=1.0Plots.Measures.cm,
           rightmargin=1.0Plots.Measures.cm,
           bottommargin=1.0Plots.Measures.cm,
       )

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/e/7ec191febde5f2eab0833a8614bb326785eb5f46.png)

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [July 5, 2024, 12:12pm UTC](https://discourse.julialang.org/t/some-questions-about-plots/116649/5 "2024-07-05T12:12:21Z")

</div>

And you can also set `topmargin` to a negative number to get the title closer to the plot.

---

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [July 5, 2024, 12:22pm UTC](https://discourse.julialang.org/t/some-questions-about-plots/116649/6 "2024-07-05T12:22:43Z")

</div>

Thanks, Professor! It does work though I can’t think it’s a good experience. 😓

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [July 5, 2024, 12:26pm UTC](https://discourse.julialang.org/t/some-questions-about-plots/116649/7 "2024-07-05T12:26:51Z")

</div>

There are other backends (pyplot, pgfplotx, etc) that might do things differently and more to your taste. And of course the Makie option.

Personally, I always find that tuning plot properties requires fiddling around with the details, no matter the tool, and at the end I might even save a SVG and to to Inkscape for fine tuning.

---

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [July 5, 2024, 12:34pm UTC](https://discourse.julialang.org/t/some-questions-about-plots/116649/8 "2024-07-05T12:34:29Z")

</div>

Thanks! At least so far, I think `Makie` works better with me.
