# Plotting the Mandelbrot set at different zoom levels

I'm interested in making an animated movie of a zoom in on a part of the Mandelbrot set. My code works well for a few zooms, but upon trying to zoom in quite far, I find that the fractal becomes "smoothed out". Am I missing something that prevents me from seeing the fractal structure at higher zoom levels? Am I hitting machine precision in the computations? Here's what I'm talking about:

Zoom level 3:

Zoom level 9:

Zoom level 20:

The first plot looks good, the second is okay, and the third is not fractal at all.

If there are any other deficiencies or improvements I'd be glad to hear about them as well.

Here's my code:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib import animation
import time

# some interesting places in the set
# http://www.nahee.com/Derbyshire/manguide.html

N = 500
nIts = 25
nZooms = 50
x0=0
y0=-1

movie = np.zeros([N,N,nZooms])

x = np.linspace(-2,1,N)
y = np.linspace(-1,1,N)
X,Y = np.meshgrid(x,y)
c = X + 1j*Y
z = 0*c
for i in range(nIts):
z = z**2 + c

mask = np.abs(z) < 1
z[z>1]=0
z[np.isnan(z)]=0

# plotting stuff
for j in range(1,nZooms):
h=1./(2**j)
print "Plot number ", j
x = x0+h*np.linspace(-1,1,N)
y = y0+h*np.linspace(-1,1,N)
X,Y = np.meshgrid(x,y)
c = X + 1j*Y
z = 0*c
for i in range(nIts):
z = z**2 + c

mask = np.abs(z) < 1
z[z>1]=0
z[np.isnan(z)]=0

fig = plt.figure()

for j in range(nZooms):
name = "image%d.png" % j
plt.imshow(movie[:,:,j], cmap = 'RdBu')
plt.gray()
plt.axis('equal')
plt.axis('off')
# plt.show()
fig.savefig(name)
time.sleep(1)

• Are you seeking a general review to improve any and all aspects of your code, or just to address that specific concern? – Phrancis Feb 16 '17 at 23:43
• @Phrancis If there are any other deficiencies, I'd be glad to hear about them. – Matt Feb 17 '17 at 0:45
• Never seen code that uses x and X. Would vote seriously against doing that. – MKesper Feb 17 '17 at 9:55
• Are you sure the numeric type you're using has enough precision for the zoom level you want? If not, that seems like a reasonable reason for it not being fractal at all. – Fund Monica's Lawsuit Mar 29 '17 at 23:18
• In fact, scale the maximum iteration count linearly with the zoom depth. That should help. – Tamoghna Chowdhury Mar 30 '17 at 7:03