2

I am trying to plot Mandelbrot set using matplotlib. I've done it with one color, but now i am trying to put in some cmap depending on number of iterations. So, my code looks like:

import random
import matplotlib.pyplot as plt


elem = 10000
x = []
y = []
colors = []

def iteration(x):
    n = 0
    result = 0
    while n < 1000: # max iterations
        result = pow(result, 2) + c
        if abs(result) > 2:
            return n           
        n += 1
    return n

for i in range(elem):
    c = complex(random.uniform(-2.5,1),random.uniform(-1.5,1.5))
    x.append(c.real)
    y.append(c.imag)
    colors.append(iteration(c))


plt.scatter(x, y, ',', c = colors, cmap= 'magma', vmin = 0, vmax = 1000)
plt.axis('equal')
plt.axis([-2,1,-1.5,1.5])
plt.title('Mandelbrot Set', y = 1.05)
plt.ylabel('Im')
plt.xlabel('Re')
plt.colorbar()
plt.show()

I am not sure how to define c, vmin and vmax in plt.scatter. I would like to have, for example if n is 1000 (max iteration) black dot and another color if n = 0.

1

1 Answer 1

2

I do think your code works very well. Just need to define the marker.

plt.scatter(x, y, marker=',', c = colors, cmap= 'magma', vmin = 0, vmax = 1000)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.