Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding global title to Plots.jl subplots

Tags:

julia

plots.jl

I would like to add a global title to a group of subplots using Plots.jl.

Ideally, I'd do something like:

using Plots
pyplot()
plot(rand(10,2), plot_title="Main title", title=["A" "B"], layout=2)

but, as per the Plots.jl documentation, the plot_title attribute is not yet implemented:

Title for the whole plot (not the subplots) (Note: Not currently implemented)

In the meanwhile, is there any way around it?

I'm currently using the pyplot backend, but I'm not especially tied to it.

like image 698
Nicolas Payette Avatar asked Mar 28 '17 10:03

Nicolas Payette


People also ask

How to set title for subplots in figure?

Setting a title for just one plot is easy using the title() method. By using this function only the individual title plots can be set but not a single title for all subplots. Hence, to set a single main title for all subplots, suptitle() method is used.

How to give title to subplot in Matplotlib?

We can also add title to subplots in Matplotlib using title. set_text() method, in similar way to set_title() method.


2 Answers

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

like image 180
Alec Avatar answered Oct 15 '22 10:10

Alec


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

like image 5
Matt Krause Avatar answered Oct 15 '22 09:10

Matt Krause