Problem Using annotate! to add info to an animated plot but it overlaps and cannot be read

Hi I am triying to use !annotate() to add text info like current iteration, aproximation and relative error to an animation of Newton-Raphson method but the previous info is accumulating in the new frames and overlaps. How can I avoid that to be able to see the info propperly?

Blockquote

function animate_newton_raphson(df, f_expr, vars_vec, x0, xcoor, ycoor, ts_flat, sympy_expr)

# Create LaTeX string of sympy_expr
latex_expr = latexify(sympy_expr)
#x0i=convert(Float64,x0)
p = plot(f_expr, x0i:0.01:x0d, label="f(x)", color=:navy, title= " Newton-Raphson Method", grid= false)

for i in 1:size(df, 1)


    if i == 1
        scatter!(p, [x0[1]], [f_expr(x0[1])], markersize=4, markerstrokecolor=:black,markerstrokewidth=0.5, color=:red, label="", legend=:none)
        
    else
        scatter!(p, [df.vars[i]], [df.f_x[i]], markersize=4, markerstrokecolor=:black, markerstrokewidth=0.5, color=:green, label="", legend=:none)
        plot_lines_to_xaxis(xcoor, ycoor, ts_flat, p)
        
    end
    annotate!(x0d,5,text("$(LIteration) $(riterc[i])\n $(Lapprox) $(rxcoor[i])\n $(Lerror) $(rcrel_err[i])",
    halign=:right, valign=:bottom, pointsize=8))
   
end
return p

end

animation = @animate for i=1:size(df, 1)
animate_newton_raphson(df[1:i, :], f_expr, vars_vec, x0, xcoor, ycoor, ts_flat, sympy_expr)
end

gif(animation, “3N-R_converg.gif”, fps=10)

NR

Assuming you are using Plots.jl, you could try workaround as per this example:

using Printf, Plots; gr()

plot(cos, -5:0.1:5)
str0 = "█"^6
for i in 0:50:1000
    stri = @sprintf("Iteration: %5i", i)
    annotate!([5],[1], text(str0, :white, halign=:right))
    display(annotate!([5],[1], text(stri, halign=:right, pointsize=8)))
    sleep(0.1)
end
1 Like