2

我想创建多个带有各自图例和共享 y 轴限制的子图。我目前正在通过执行以下操作在循环中创建子图:

fig = Figure()
    
for i in 1:3
    lines(fig[i, 1], rand(10), label="$i")
end
    
linkyaxes!(fig.content...)
    
fig

这很好用,但是当接下来尝试向每个子图添加图例时:

fig = Figure()
    
for i in 1:3
    lines(fig[i, 1], rand(10), label="$i")
    axislegend()
end
    
linkyaxes!(fig.content...)
    
fig

这现在会引发错误:

MethodError: Cannot `convert` an object of type Makie.MakieLayout.Legend to an object of type Makie.MakieLayout.Axis

因为除了之前的原始对象之外,fig.content现在还包括对象。Makie.MakieLayout.Legend()Axis

我需要事先过滤掉这些,还是有更好的方法来生成所需的图?

4

1 回答 1

2

我不确定这是最好的方法,但是您可以确保将轴传递给linkyaxes!这种方式:

axs = []

fig = Figure()

for i in 1:3
    ax = lines(fig[i, 1], rand(10), label="$i").axis
    push!(axs, ax)
    axislegend()
end

linkyaxes!(axs...)
于 2021-07-09T01:37:02.010 回答