Trying to plot Mandelbrot set - no matter what, always get plain black image
<p>First, here's the code I have:</p>

<pre><code>from PIL import Image as im
import numpy as np 

def mandelbrot_iteration(c): 
    iters = 0
    while abs(c)&lt;2 and iters&lt;200:
        c=c**2+c
        iters+=1
    return iters

HEIGHT = 400
WIDTH = 500

diag = im.new('L',(WIDTH, HEIGHT))
pix = diag.load()

x_pts = np.arange(-2,2,4/WIDTH)  
y_pts = np.arange(-2,2,4/HEIGHT)

for x in x_pts:
    for y in y_pts:
        pix[x+2,y+2]=mandelbrot_iteration(complex(x,y))

diag.save("Fractal.png")
</code></pre>

<p>I thought this was quite straight forward. I see how many interations each point on a grid of complex numbers takes to grow past an abs. value of 2 and plot these values as a colour at each point (with 200 being the cutoff, assuming the sequence doesn't diverge). In the range specified, there should definitely be some non-trivial things going on, but no matter what I try, the image made is plain black. </p>

<p>Also this method of generating images has almost zero documentation. I've searched a lot, and this: </p>

<blockquote>
  <p>im.load()</p>
  
  <p>Allocates storage for the image and loads it from the file (or from
  the source, for lazy operations). In normal cases, you donât need to
  call this method, since the Image class automatically loads an opened
  image when it is accessed for the first time.</p>
  
  <p>(New in 1.1.6) In 1.1.6 and later, load returns a pixel access object
  that can be used to read and modify pixels. The access object behaves
  like a 2-dimensional array, so you can do:</p>
  
  <p>pix = im.load() print pix[x, y] pix[x, y] = value</p>
  
  <p>Access via this object is a lot faster than getpixel and putpixel</p>
</blockquote>

<p>Is <em>everything</em> that I can find about it (no examples out there either), which is very frustrating. I imagine the line pix[x+2,y+2] is at fault. The '+2's are there to stop the "out of range" errors, but, having tried some examples, I have no idea what it does with input numbers to generate a colour. I did find that 'L' when the image is created should make a greyscale image, but no idea what range pix[x,y] expects or anything. Everything came out black...</p>

<p>The immediate problem is your <em>scale</em> is off.</p>

<p>In this line <code>pix[x+2,y+2]=...</code>, with your ranges for <code>x</code> and <code>y</code>, the only pixels that are being drawn are <code>0..4</code>. Since the last few pixels drawn are black, the entire top left 4x4 square is black (and the rest is 0 â also black â by default, for a new image).</p>

<p>That can be fixed like this:</p>

<pre><code>from PIL import Image as im
import numpy as np 

def mandelbrot_iteration(c): 
    iters = 0
    while abs(c)&lt;2 and iters&lt;200:
        c=c**2+c
        iters+=1
    return iters

HEIGHT = 400
WIDTH = 500

diag = im.new('L',(WIDTH, HEIGHT))
pix = diag.load()

x_pts = np.arange(-2,2,4.0/WIDTH)  
y_pts = np.arange(-2,2,4.0/HEIGHT)

for x in x_pts:
    for y in y_pts:
        pix[WIDTH*(x+2)/4.0,HEIGHT*(y+2)/4.0]=mandelbrot_iteration(complex(x,y))

diag.show()
</code></pre>

<p>although the result is not yet a good Mandelbrot...</p>

<p><a href="http://i.stack.imgur.com/kgqoH.png" rel="nofollow"><img src="http://i.stack.imgur.com/kgqoH.png" alt="mandelsmorgasbord"></a></p>

<p>With hcs' comment "mandelbrot iteration should be z=0, while abs(z)&lt;2, z=z**2+c" applied, you'd use this code</p>

<pre><code>from PIL import Image as im
import numpy as np 

def mandelbrot_iteration(c): 
    iters = 0
    z = 0
    while abs(z)&lt;2 and iters&lt;200:
        z=z**2+c
        iters+=1
    return iters

HEIGHT = 400
WIDTH = 500

diag = im.new('L',(WIDTH, HEIGHT))
pix = diag.load()

x_pts = np.arange(-2,2,4.0/WIDTH)  
y_pts = np.arange(-2,2,4.0/HEIGHT)

for x in x_pts:
    for y in y_pts:
        pix[WIDTH*(x+2)/4.0,HEIGHT*(y+2)/4.0]=mandelbrot_iteration(complex(x,y))

# diag.show()
diag.save("Fractal.png")
</code></pre>

<p>and lo and behold, a true Mandelbrot pops up:</p>

<p><a href="http://i.stack.imgur.com/NOo72.png" rel="nofollow"><img src="http://i.stack.imgur.com/NOo72.png" alt="Correct Mandelbrot"></a></p>

python python-imaging-library