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

ult*_*ian 3 heatmap subplot julia plots.jl

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

a = randn(5,5,8)
a = a.-mean(a)
a = a./maximum(extrema(a))
Run Code Online (Sandbox Code Playgroud)

如果我想将一些 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)))
Run Code Online (Sandbox Code Playgroud)

它产生:

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
Run Code Online (Sandbox Code Playgroud)

这里最好的方法是什么?

Chr*_*kas 6

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

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...)
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为设置

p1 = plot(...)
p2 = plot(...)
plot(p1,p2)
Run Code Online (Sandbox Code Playgroud)

创建一个带有子图 p1 和 p2 的图,所以我们只是将它用于任意数量的图。您也可以在此处设置布局,尽管使用任意数量可能会更困难。

  • 谢谢你帮我理直了头。关键是直接喷出一个数组。因此,将我的尝试更改为:`plot([heatmap(a[:,:,i], aspect_ratio=:equal, clims=(-1,1)) for i in 1:8]...)` 将起作用,因为splat [在函数调用中拆分参数](http://docs.julialang.org/en/stable/manual/faq/#splits-one-argument-into-many-different-arguments-in-function-calls)。 (2认同)