【问题标题】:How to create an arbitrary number of subplots in Julia Plots如何在 Julia Plots 中创建任意数量的子图
【发布时间】:2017-05-02 22:27:39
【问题描述】:

我想使用 Breloff 的 Julia Plots 从多维数组中制作一组子图。这个绘图函数接受一个可变参数输入并将它们变成子图,但我似乎无法正确输入我的数组,并且可能忽略了一些简单的东西。例如数组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)))

产生:

但是,如果我想全部 8 个(或者我的目标是可变数字),我不能让它与循环或 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

这里最好的方法是什么?

【问题讨论】:

    标签: julia heatmap subplot plots.jl


    【解决方案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 的图,因此我们只是将它与任意数量的图一起使用。您也可以在此处设置布局,但使用任意数量可能会更困难。

    【讨论】:

    • 感谢您帮助我理清头绪。关键是直接喷出一个数组。因此,将我的尝试更改为:plot([heatmap(a[:,:,i], aspect_ratio=:equal, clims=(-1,1)) for i in 1:8]...) 将起作用,因为 splat splits the arguments in function calls
    • plot(plot_array...,layout=length(plot_array)) 用于基本布局
    猜你喜欢
    • 2020-04-15
    • 1970-01-01
    • 2022-07-12
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多