如何在 Julia Plots 中创建任意数量的子图

How to create an arbitrary number of subplots in Julia Plots

我想使用 Breloff 的 Julia Plots 从多维数组制作一组子图。这个 plot 函数接受一个可变参数输入并将它们变成子图,但我似乎无法正确地输入我的数组并且可能忽略了一些简单的东西。例如数组 a:

a = randn(5,5,8)
a = a.-mean(a)
a = a./maximum(extrema(a))

如果我想将一些 5x5 切片绘制为热图,我可以这样做:

plot(heatmap(a[:,:,1], aspect_ratio=:equal, clims=(-1,1), title=string(1)), 
heatmap(a[:,:,2], aspect_ratio=:equal, clims=(-1,1), title=string(2)),
heatmap(a[:,:,3], aspect_ratio=:equal, clims=(-1,1), title=string(3)))

产生:

但如果我想完成所有八项(或可变数字,这是我的目标),我无法使其与循环或 splat 一起工作。我尝试使用后者创建一个元组,但出现错误:

plot(tuple([heatmap(a[:,:,i], aspect_ratio=:equal, clims=(-1,1)) for i in 1:8]...))

LoadError: MethodError: Cannot `convert` an object of type String to an object of type MethodError
This may have arisen from a call to the constructor MethodError(...),
since type constructors fall back to convert methods.
while loading In[1], in expression starting on line 1

最好的方法是什么?

我认为这里最简单的方法是制作单独的图,然后将它们放在一起形成最终图。您可以在一个循环中制作一组绘图:

plot_array = Any[] # can type this more strictly
for i in 1:n
  push!(plot_array,plot(...)) # make a plot and add it to the plot_array
end
plot(plot_array...)

之所以有效,是因为设置

p1 = plot(...)
p2 = plot(...)
plot(p1,p2)

创建一个包含子图 p1 和 p2 的图,因此我们只是将其与任意数量的图一起使用。您也可以在此处设置布局,但任意数量的设置可能会更加困难。