如何在Julia Plots中创建任意数量的子图 [英] How to create an arbitrary number of subplots in Julia Plots

查看:99
本文介绍了如何在Julia Plots中创建任意数量的子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Breloff的茱莉亚(Julia)从多维数组制作一组子图

I want to make a set of subplots from a multidimensional array using Breloff's Julia Plots. This plot function takes a varargs input and makes them into subplots, but I can't seem to feed my array in properly and am probably overlooking something simple. For example with an array a:

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

如果我想将一些5x5切片绘制为热图,则可以执行以下操作:

If I want to plot some of the 5x5 slices as heatmaps, I can do:

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一起使用.我尝试使用后者创建一个元组,但出现错误:

but if I want to do all eight (or a variable number which is my goal), I can't make it work with a loop or a splat. I tried the latter to create a tuple but got an error:

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

这里最好的方法是什么?

What is the best approach here?

推荐答案

我认为这里最简单的方法是制作单独的图,然后将它们放在一起成为最终图.您可以循环制作一系列图:

I think the easiest way here is to make the separate plots and then put them together in a final plot. You can make an array of plots in a loop:

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的图,因此我们仅将其与任意数量的图一起使用.您也可以在此处设置布局,尽管使用任意数量可能会更困难.

creates a plot with subplots p1 and p2, and so we are just using that with an arbitrary amount of plots. You can also set layouts here, though that may be more difficult with arbitrary amounts.

这篇关于如何在Julia Plots中创建任意数量的子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆