Adding global title to Plots.jl subplots

More recent versions of Plots.jl support the plot_title attribute, which provides a title for the whole plot. This can be combined with individual titles of individual plots.

using Plots   

layout = @layout [a{0.66w} b{0.33w}]
LHS = heatmap(rand(100, 100), title="Title for just the heatmap")
RHS = plot(1:100, 1:100, title="Only the line")
plot(LHS, RHS, plot_title="Overall title of the plot")

Alternatively, you can set the title for an existing plot directly.

p = plot(LHS, RHS)
p[:plot_title] = "Overall title of the plot"
plot(p)

Example plot, produced by the code above


When using the pyplot backend, you can use PyPlot commands to alter a Plots figure, cf. Accessing backend specific functionality with Julia Plots.

To set a title for the whole figure, you could do something like:

using Plots
p1 = plot(sin, title = "sin")
p2 = plot(cos, title = "cos")
p = plot(p1, p2, top_margin=1cm)
import PyPlot
PyPlot.suptitle("Trigonometric functions")
PyPlot.savefig("suptile_test.png")

One needs to explicitly call PyPlot.savefig to see the effect of the PyPlot functions.

Note that all changes made using the PyPlot interface will be overwritten when you use a Plots function.


This is a bit of a hack, but should be agnostic to the backend. Basically create a new plot where the only contents are the title you want, and then add it on top using layout. Here is an example using the GR backend:

# create a transparent scatter plot with an 'annotation' that will become title
y = ones(3) 
title = Plots.scatter(y, marker=0,markeralpha=0, annotations=(2, y[2], Plots.text("This is title")),axis=false, grid=false, leg=false,size=(200,100))

# combine the 'title' plot with your real plots
Plots.plot(
    title,
    Plots.plot(rand(100,4), layout = 4),
    layout=grid(2,1,heights=[0.1,0.9])
)

Produces:

enter image description here

Tags:

Julia

Plots.Jl