Some questions about `Plots`

  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?
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

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

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
       )

1 Like

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

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

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

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

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> 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,
       )

1 Like

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

1 Like

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

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.

1 Like

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