Super title in Plots and subplots

Hi,

I am having a hard time understanding how to organize titles in Plots.jl using pyplot().

I have 2X2 subplots and I would like to have a title for each subplot, one for each horizontal pair, and one for the overall plot. How can I do that?

Thanks

Hi,

as you probably figured out, there is no problem of having a title per plot.
It is set by a simple title option.

The others are not possible to add by Plots.jl options as far as I can tell.
At least, you can always get a matplotlib object and act on it directly.

using Plots
pyplot()

plot(layout=grid(2,2))
plot!(x->x^2, -1, 2, sp=1, title="Title 1", frame=:box)
plot!(x->x^2, -1, 2, sp=2, title="Title 2", frame=:box)
plot!(x->x^2, -1, 2, sp=3, title="Title 3", frame=:box)
plot!(x->x^2, -1, 2, sp=4, title="Title 4", frame=:box)

plt = plot!()
fig = plt[3][1][:plot_object].o
children = fig[:get_children]()
ax = children[2]
ax[:text](0.6, 1.2, "My Big Title", fontsize=22, transform=ax[:transAxes], verticalalignment="bottom")
fig[:canvas][:print_figure]("awesome.pdf",bbox_inches="tight")

It feels ugly, I know.

Hi,

Thanks. Just to be clear, this code generates a title for the whole plot, but not for each row, right?

The documentation mentions a plot_title attribute which has not yet been implemented, but I assume it will be eventually. In the meantime, you can use a subplot containing only a title as a workaround:

using Plots
title = plot(title = "Plot title", grid = false, showaxis = false, bottom_margin = -50Plots.px)
p1 = scatter(rand(5, 1), title = "Subplot 1")
p2 = scatter(rand(5, 1), title = "Subplot 2")
plot(title, p1, p2, layout = @layout([A{0.01h}; [B C]]))

title

5 Likes

Thanks, this will do. Still surprised that Plots is missing very basic features.

6 Likes