我想使用 Plots.jl 为一组子图添加全局标题。
理想情况下,我会这样做:
using Plots
pyplot()
plot(rand(10,2), plot_title="Main title", title=["A" "B"], layout=2)
Run Code Online (Sandbox Code Playgroud)
但是,根据Plots.jl 文档,该plot_title属性尚未实现:
整个情节的标题(不是子情节)(注意:当前未实施)
同时,有什么办法可以解决吗?
我目前正在使用pyplot后端,但我并没有特别依赖它。
Plots.jl 的最新版本支持该plot_title属性,该属性为整个绘图提供标题。这可以与各个地块的各个标题相结合。
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")
Run Code Online (Sandbox Code Playgroud)
或者,您可以直接为现有绘图设置标题。
p = plot(LHS, RHS)
p[:plot_title] = "Overall title of the plot"
plot(p)
Run Code Online (Sandbox Code Playgroud)
这有点黑客,但应该与后端无关。基本上创建一个新图,其中唯一的内容是您想要的标题,然后使用layout. 这是使用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, 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])
)
Run Code Online (Sandbox Code Playgroud)
产生:
使用pyplot后端时,您可以使用PyPlot命令来更改Plots图形,参见。使用 Julia Plots 访问后端特定功能。
要为整个图设置标题,您可以执行以下操作:
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")
Run Code Online (Sandbox Code Playgroud)
需要显式调用PyPlot.savefig才能看到函数的效果PyPlot。
请注意,当您使用函数时,使用界面所做的所有更改都PyPlot将被覆盖Plots。