How record a chart animation function

Based on this, how about this approach:

using LaTeXStrings
using GLMakie
using DataFrames

function simulation_data(nSimul, AreaSquare)
    df = DataFrame(
        x = rand(nSimul) * 10,
        y = rand(nSimul) * 100,
        Fx = 0.00,
        Δy = 0.00,
        up_x = -1.00,
        up_y = -1.00,
        dw_x = -1.00,
        dw_y = -1.00,
    )
    # df.x .- df.y

    #Up or Down of the function y?
    df.Fx = df.x .^ 2
    df.Δy = df.y .- df.Fx
    for i = 1:nSimul
        if df.Δy[i] .> 0
            df.up_x[i] = df.x[i]
            df.up_y[i] = df.y[i]
        else
            df.dw_x[i] = df.x[i]
            df.dw_y[i] = df.y[i]
        end
    end
    contXYdw = nrow(df[df.Δy .<= 0, :])

    #Calc integral
    integral = (contXYdw / nSimul) * AreaSquare
    integralCorreta = (10^3) / 3

    #loop for i to generate the steps in a dataframe
    dfIntegral = DataFrame(n = 1:nSimul, cont = 0, valor = 0.00, erro = 0.00, Δ = 0.00)
    cont = 0
    area = 0
    for i = 1:nSimul
        if df[i, "Δy"] <= 0
            cont = cont + 1
        end
        area = (cont / i) * AreaSquare
        dfIntegral.cont[i] = cont
        dfIntegral.valor[i] = area
        dfIntegral.erro[i] = area - integralCorreta
        if i > 1
            dfIntegral.Δ[i] = dfIntegral.valor[i] - dfIntegral.valor[i - 1]
        end
    end
    return df, dfIntegral
end

# Now, plotting the dataframe per i

function geraPlot(df, dfIntegral, AreaSquare) #function that update the chart to each i points

    nSimul = size(df, 1)
    #i=10
    fig = Figure()
    # Graf 1

    i = Observable(1)

    up_pts = @lift Point2f.(df.up_x[1:($i)], df.up_y[1:($i)])
    dw_pts = @lift Point2f.(df.dw_x[1:($i)], df.dw_y[1:($i)])

    ax1 =
        fig[1:4, 1] = Axis(
            fig,
            xlabel = L"x",
            ylabel = L"f (x)",
            xgridstyle = :dash,
            ygridstyle = :dash,
        )

    x = 0:10
    y = x .^ 2
    lines!(x, y; label = L"x^{2}")
    band!(
        x,
        fill(0, length(x)),
        y;
        color = (:blue, 0.4),
        label = L"A (x) = \int_0^{10} x^{2}dx",
    )
    scatter!(up_pts, rasterize = true, markersize = 4.0, color = (:gray, 1.0), label = "∉A")
    scatter!(dw_pts, rasterize = true, markersize = 4.0, color = (:red, 1.0), label = "∈A")

    xlims!(0, 10)
    ylims!(0, 100)
    #axislegend("Legend"; position = :rt, bgcolor = (:grey90, 0.25))

    fig[1, 2] = Legend(fig, ax1, "Legend", bgcolor = (:grey90, 0.25), framevisible = false)
    #display(fig)

    # Graf 3 - Integral Result x nSimulation
    valor_pts = @lift Point2f.(dfIntegral.n[1:($i)], dfIntegral.valor[1:($i)])
    ax3 = fig[2, 2:3] = Axis(fig, title = "Integral value (A)")
    lines!(valor_pts)
    xlims!(0, nSimul + 10)
    A_yMax = AreaSquare
    A_yMin = 0
    ylims!(A_yMin, A_yMax)

    # Graf 4 - Erro x nSimulation
    erro_pts = @lift Point2f.(dfIntegral.n[1:($i)], dfIntegral.erro[1:($i)])
    ax4 = Axis(fig[3, 2:3], title = "Erro")
    lines!(erro_pts)
    xlims!(0, nSimul + 10)
    er_yMax = maximum(dfIntegral.erro)
    er_yMin = minimum(dfIntegral.erro)
    ylims!(er_yMin, er_yMax)

    # Graf - Δ Resultado x nSimulation
    Δ_pts = @lift Point2f.(dfIntegral.n[1:($i)], dfIntegral.Δ[1:($i)])
    ax5 = Axis(fig[4, 2:3], title = "Δ = A(i) - A(i-1)", xlabel = "n points")
    lines!(Δ_pts)
    xlims!(0, nSimul + 10)
    Δ_yMax = maximum(dfIntegral.Δ)
    Δ_yMin = minimum(dfIntegral.Δ)
    ylims!(Δ_yMin, Δ_yMax)
    #display(fig)
    return fig, i
end

function make_animation(output_filename, nSimul = 100, AreaSquare = 10 * 10^2)
    df, dfIntegral = simulation_data(nSimul, AreaSquare)
    figure, index = geraPlot(df, dfIntegral, AreaSquare)

    record(figure, output_filename, 1:nSimul; framerate = 30) do i
        index[] = i
    end
end

make_animation("test.gif")

test

5 Likes