使用Plots.jl有效地对子图进行动画处理 [英] Animating subplots using Plots.jl efficiently

查看:72
本文介绍了使用Plots.jl有效地对子图进行动画处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有GR后端的Plots.jl在Julia中创建具有三个子图(一个曲面,两个热图)的动画.到目前为止,我的代码中最慢的部分是这些图的生成,因此我试图找到最有效的方法.

I am trying to create an animation with three subplots (one surface, two heatmaps) in Julia using Plots.jl with the GR backend. By far the slowest part of my code is the generation of these plots, so I am trying to find the most efficient way to do it.

我尝试重新调用动画循环内的绘图,但这比在原处进行修改要慢得多:

I've tried re-calling the plotting inside the animate loop, but that was significantly slower than modifying in place as so:

using Plots,Profile

function mcve(n)
    A = rand(n,100,100)
    B = rand(n,100,100)

    l = @layout [ a b ; c]
    p1 = surface(1:100,1:100,A[1,:,:],clims=(0,1),legend=false)
    p2 = heatmap(A[1,:,:],clims=(0,1),aspect_ratio=1,legend=false)
    p3 = heatmap(B[1,:,:],aspect_ratio=1)
    p = plot(p1,p2,p3,layout = l)

    anim = @animate for i=1:n
        surface!(p[1],1:100,1:100,A[i,:,:])
        heatmap!(p[2],A[i,:,:])
        heatmap!(p[3],B[i,:,:])
    end
    gif(anim,"example.gif")
end

mcve(1)
@profile mcve(10)
Profile.print()

跟踪结果 https://pastebin.com/Lv9uCLE5

根据探查器,将近一半的运行时间都花在了一个名为"setcharheight"的函数上,该函数正在调用c库.有没有办法减少我需要拨打的电话的数量?

According to the profiler, nearly half the runtime is spent in a function "setcharheight", which is calling a c library. Is there a way to reduce the number of calls to it I need to make?

推荐答案

我做了一些实验,发现有两件事可以极大地加快绘图过程.

I did some experiments, and I found two things that could dramatically speed up the plotting process.

首先,我没有使用surface!()和heatmap!()重绘绘图,而是简单地替换了它们的:z系列.通过将代码示例中的第一,第三和第二与第四功能进行比较,可以看出这一点.

First, rather than redrawing the plots using surface!() and heatmap!(), I simply replaced their :z series. This is shown by comparing the first to the third and second to the fourth functions in the code example.

第二,GR.jl;setcharheight非常慢.这可能是由于ccall()所致,这意味着它可能取决于操作系统.通过将xticks和yticks设置为false,可以显着提高速度.通过比较示例中的第一到第二功能以及第三到第四功能,可以看出这一点.

Second, GR.jl; setcharheight is extremely slow. This is likely due to the ccall(), which means it may be OS dependent. By setting xticks and yticks to false, significant speedups were achieved. This is shown by comparing the first to second and third to fourth functions in the example.

using Plots

function mcve(n,A,B)

    l = @layout [ a b ; c]
    p1 = surface(1:100,1:100,A[1,:,:],clims=(0,1),legend=false)
    p2 = heatmap(A[1,:,:],clims=(0,1),aspect_ratio=1,legend=false)
    p3 = heatmap(B[1,:,:],aspect_ratio=1)
    p = plot(p1,p2,p3,layout = l)

    anim = @animate for i=1:n
        surface!(p[1],1:100,1:100,A[i,:,:])
        heatmap!(p[2],A[i,:,:])
        heatmap!(p[3],B[i,:,:])
    end
    gif(anim,"example1.gif")
end

function mcve4(n,A,B)

    l = @layout [ a b ; c]
    p1 = surface(1:100,1:100,A[1,:,:],clims=(0,1),legend=false,xticks=false,yticks=false)
    p2 = heatmap(A[1,:,:],clims=(0,1),aspect_ratio=1,legend=false,xticks=false,yticks=false)
    p3 = heatmap(B[1,:,:],aspect_ratio=1,xticks=false,yticks=false)
    p = plot(p1,p2,p3,layout = l)

    anim = @animate for i=1:n
        surface!(p[1],1:100,1:100,A[i,:,:],xticks=false,yticks=false)
        heatmap!(p[2],A[i,:,:],xticks=false,yticks=false)
        heatmap!(p[3],B[i,:,:],xticks=false,yticks=false)
    end
    gif(anim,"example4.gif")
end


function mcve2(n,A,B)

    l = @layout [ a b ; c]
    p1 = surface(1:100,1:100,A[1,:,:],clims=(0,1),legend=false,xticks =false,yticks= false)
    p2 = heatmap(A[1,:,:],clims=(0,1),aspect_ratio=1,legend=false,xticks = false,yticks= false)
    p3 = heatmap(B[1,:,:],aspect_ratio=1,xticks = false,yticks= false)
    p = plot(p1,p2,p3,layout = l)

    anim = @animate for i=1:n
        p[1][1][:z] = A[i,:,:]
        p[2][1][:z] = A[i,:,:]
        p[3][1][:z] = B[i,:,:]
    end
    gif(anim,"example2.gif")
end


function mcve3(n,A,B)

    l = @layout [ a b ; c]
    p1 = surface(1:100,1:100,A[1,:,:],clims=(0,1),legend=false)
    p2 = heatmap(A[1,:,:],clims=(0,1),aspect_ratio=1,legend=false)
    p3 = heatmap(B[1,:,:],aspect_ratio=1)
    p = plot(p1,p2,p3,layout = l)

    anim = @animate for i=1:n
        p[1][1][:z] = A[i,:,:]
        p[2][1][:z] = A[i,:,:]
        p[3][1][:z] = B[i,:,:]
    end
    gif(anim,"example3.gif")
end

A = rand(1,100,100)
B = rand(1,100,100)

mcve(1,A,B)
mcve2(1,A,B)
mcve3(1,A,B)
mcve4(1,A,B)

A = rand(10,100,100)
B = rand(10,100,100)

println("Replot,ticks on")
@time mcve(10,A,B)
println("Replot,ticks off")
@time mcve4(10,A,B)
println(":z replace, ticks on")
@time mcve3(10,A,B)
println(":z replace, ticks off")
@time mcve2(10,A,B)

这将导致

Replot,ticks on
 19.347849 seconds (12.78 M allocations: 399.848 MiB, 0.30% gc time)
Replot,ticks off
  6.227432 seconds (8.71 M allocations: 298.890 MiB, 0.88% gc time)
:z replace, ticks on
  8.572728 seconds (5.43 M allocations: 149.359 MiB, 0.24% gc time)
:z replace, ticks off
  1.805316 seconds (1.36 M allocations: 48.450 MiB, 0.40% gc time)

这篇关于使用Plots.jl有效地对子图进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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