# Mandelbrot with Plots.jl

**URL:** <https://discourse.julialang.org/t/mandelbrot-with-plots-jl/52410>\
**Category:** New to Julia\
**Tags:** plotting, glmakie\
**Created:** [December 26, 2020, 4:55pm UTC](https://discourse.julialang.org/t/mandelbrot-with-plots-jl/52410 "2020-12-26T16:55:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ayush\_Rawat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ayush_rawat/32/18961_2.png) [@Ayush\_Rawat](https://discourse.julialang.org/u/Ayush_Rawat)\
**Post date:** [December 26, 2020, 4:55pm UTC](https://discourse.julialang.org/t/mandelbrot-with-plots-jl/52410/1 "2020-12-26T16:55:13Z")

</div>

I’m trying to do a crude sketch of the Mandelbrot set to better my understanding of Plots.jl. But the code is taking way too long to execute. Thus I humbly ask for help.  
My class is as follows (the `begin` and `end` I use because I’m working on Pluto):

```julia-auto
begin
	Base.@kwdef mutable struct frac
	z::Complex=0+0im
	c::Complex
	end
	
	function iterate!(f::frac)
		f.z=f.z^2+f.c
	end
end

```

And then I plot the graph by iterating through a discretized plane:

```julia-auto
begin
	ploot=plot(1,xlim = (-2,2),ylim = (-2,2),title="mandelbrot iterator")
@gif for x=-2:1e-1:2,y=-1:1e-2:2
	mandel=frac(c=x+y*im)
	map(x->iterate!(mandel),(1:7))
	if(abs(mandel.z)<2)
		push!(ploot,x,y)
	end
end
end

```

This code has been running for at least an hour on my machine (6th gen i5+4GB GTX960 with tons of RAM). From my reading I’ve understood that my code isn’t leveraging the GPU, but I’m unable to think of a way to make Plots use the GPU.

Thank You

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [December 26, 2020, 6:45pm UTC](https://discourse.julialang.org/t/mandelbrot-with-plots-jl/52410/2 "2020-12-26T18:45:21Z")

</div>

Probably the biggest issue with your code is that the struct `frac` has not concrete types - `Complex` is an abstract type, a concrete type would e.g. be `Complex{Float64}`.  
Putting the main loop not into a function is another big performance killer (here Pluto does not save you by implicitly wrapping it into a function because the code contains a macro).

Here is a version of Mandelbrot calculation I did recently, hope this helps:

[https://gist.github.com/lungben/07d342d0c7dfd4e655cccc1ecfa2922c](https://gist.github.com/lungben/07d342d0c7dfd4e655cccc1ecfa2922c)

The notebook is not very nice (yet?), but the core functionality of calculating the Mandelbrot set should be correct and reasonably fast.

---

<div class="post-metadata">

**Author:** ![Ayush\_Rawat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ayush_rawat/32/18961_2.png) [@Ayush\_Rawat](https://discourse.julialang.org/u/Ayush_Rawat)\
**Post date:** [December 26, 2020, 6:53pm UTC](https://discourse.julialang.org/t/mandelbrot-with-plots-jl/52410/3 "2020-12-26T18:53:00Z")

</div>

Thanks I’ll check it out

---

<div class="post-metadata">

**Author:** ![Ayush\_Rawat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ayush_rawat/32/18961_2.png) [@Ayush\_Rawat](https://discourse.julialang.org/u/Ayush_Rawat)\
**Post date:** [December 26, 2020, 6:54pm UTC](https://discourse.julialang.org/t/mandelbrot-with-plots-jl/52410/4 "2020-12-26T18:54:57Z")

</div>

I found the solution to be simply adding `every 10` to my for loop:

```julia
begin
	ploot=plot(1,xlim = (-2,2),ylim = (-2,2),title="mandelbrot iterator")
@gif for x=-2:1e-2:2,y=-1:1e-2:2
	mandel=frac(c=x+y*im)
	map(x->iterate!(mandel),(1:10))
	if(abs(mandel.z)<2)
		push!(ploot,x,y)
	end
	end every 150
end

```

Not the best(in fact a good contender for the shittiest), but I’ll take it. If you came here looking for a good implementation, please look at @lungben’s reply.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [December 26, 2020, 6:56pm UTC](https://discourse.julialang.org/t/mandelbrot-with-plots-jl/52410/5 "2020-12-26T18:56:38Z")

</div>

Btw. what made a large speed difference for me was replacing `abs(z) < 2` by `abs2(z) < 4`.  
Taking a square root is an expensive operation and not really required here.

---

<div class="post-metadata">

**Author:** ![Ayush\_Rawat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ayush_rawat/32/18961_2.png) [@Ayush\_Rawat](https://discourse.julialang.org/u/Ayush_Rawat)\
**Post date:** [December 26, 2020, 8:03pm UTC](https://discourse.julialang.org/t/mandelbrot-with-plots-jl/52410/6 "2020-12-26T20:03:12Z")

</div>

Ahh yess, thank you again, when I wrote this code a week ago, that was on my todo list, and it completely slipped off.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 26, 2020, 11:49pm UTC](https://discourse.julialang.org/t/mandelbrot-with-plots-jl/52410/7 "2020-12-26T23:49:04Z")

</div>

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.

```julia
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)

```

 ![mandelbrot_iterator](https://global.discourse-cdn.com/julialang/original/3X/6/c/6c969a3c598c0e58f50aff908860782a2b4f79a2.png)

---

<div class="post-metadata">

**Author:** ![Ayush\_Rawat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ayush_rawat/32/18961_2.png) [@Ayush\_Rawat](https://discourse.julialang.org/u/Ayush_Rawat)\
**Post date:** [December 27, 2020, 1:52am UTC](https://discourse.julialang.org/t/mandelbrot-with-plots-jl/52410/8 "2020-12-27T01:52:43Z")

</div>

It doesn’t work on my machine per se, but that’s probably due to installation faults on my end, which I shall try to fix

But I’m marking your answer as the solution because if anybody stumbles upon this topic, this is what they should see.
