Mandelbrot with Plots.jl

For whatever it is worth, the following modified code uses GLMakie instead of Plots and avoids plotting/collecting the images of each iteration (there are >10,000 points to be plotted in original code snippet). The function mandelbrot1 returns an Nx2 array with the solutions (and NaNs elsewhere), which is plotted once instead. It runs almost instantaneously for your original parameters.

begin
	Base.@kwdef mutable struct frac
	   z::Complex{Float64} = 0 + 0im
       c::Complex{Float64}
	end
	
	function iterate!(f::frac)
		f.z = (f.z)^2 + f.c
	end
end

function manderlbrot1(x,y)
    n, m = size(x,1), size(y,1)
    mxy = fill(NaN, m*n,2)
    k = 1;
    for xk in x, yk in y
        mandel = frac(c = xk + yk*im)
        map(x -> iterate!(mandel), 1:7)
        if abs2(mandel.z) < 4
            mxy[k,:] = [xk, yk]
            k += 1
        end
    end
    return mxy
end

using GLMakie
x, y = -2:1e-1:2, -1:1e-2:2;
mxy = manderlbrot1(x,y)
scene = Scene()
lines!(scene, mxy[:,1], mxy[:,2], color = :blue, linewidth = 1)
Makie.save("mandelbrot_iterator.png", scene)

1 Like