Adding a Global Title to Plots.jl Subheadings

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

Ideally I would do something like:

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

      

but according to the Plots.jl documentation , the attribute plot_title

is not yet implemented:

Title for the entire chart (not subheadings) (Note: Not currently implemented)

Meanwhile, is there any way around it?

I am currently using the backend pyplot

, but I am not particularly attached to it.

+4


source to share


3 answers


When using the backend, pyplot

you can use commands pyplot

to change the shape Plots

, cf. Accessing backend related functionality with Julia plots .

To set a title for the entire shape, you can 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")

      



Must be called explicitly PyPlot.savefig

to see the effect of the functions pyplot

.

Please note that all changes made using the interface pyplot

will be overwritten when using the function Plots

.

+1


source


subplots

are type fields Plot

, and each subheading has a field named :attr

that you can change and modify the display()

graph. Try the following:

julia> l = @layout([a{0.1h} ;b [c; d e]])
Plots.GridLayout(2,1)

julia> p = plot(randn(100,5),layout=l,t=[:line :histogram :scatter :steppre :bar],leg=false,ticks=nothing,border=false)

julia> p.subplots
5-element Array{Plots.Subplot,1}:
 Subplot{1}
 Subplot{2}
 Subplot{3}
 Subplot{4}
 Subplot{5}

julia> fieldnames(p.subplots[1])
8-element Array{Symbol,1}:
 :parent     
 :series_list
 :minpad     
 :bbox       
 :plotarea   
 :attr       
 :o          
 :plt

julia> for i in 1:length(p.subplots)
           p.subplots[i].attr[:title] = "subtitle $i"
       end

 julia> display(p)

      



You should now see the title in each subplot

0


source


This is a bit of a hack, but it shouldn't depend on the backend. Basically, create a new graph with the title you want as the only content and then add it at the top using layout

. Here's an example using the backend GR

:

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

0


source







All Articles