This is a simple Python program to display the whole Mandelbrot set. Well, not every point from the infinite set, just a few corresponding to pixels…

The Mandelbrot set is a mathematical set of points whose boundary is a distinctive and easily recognizable two-dimensional fractal shape. The set is closely related to Julia sets (which include similarly complex shapes), and is named after the mathematician Benoit Mandelbrot, who studied and popularized it.

Mandelbrot set images are made by sampling complex numbers and determining for each whether the result tends towards infinity when a particular mathematical operation is iterated on it. Treating the real and imaginary parts of each number as image coordinates, pixels are colored according to how rapidly the sequence diverges, if at all.

The Mandelbrot Set is a mathematical fractal defined by the recursive formula z = z^2 + c, where z and c are complex numbers.

Mandelbrot Set in Python:

This Python program plots the whole Mandelbrot set, making use of some optimisations. MIT License. Available on GitHub.

  • lower half is a reflection of the upper half;
  • the large cardioid and period 2 bulb can be omitted from further tests
__author__ = 'Alan Richmond'
'''   Copyright (C) 2014 Alan Richmond (

    Full mandelbrot set with a couple of optimizations:

    1 compute only top half, mirror it.
    2 don't compute set inside the big circle and a couple of smaller ones.
from pygame.locals import *
import pygame

def main():
    width, height = 1000,1000
    screen = pygame.display.set_mode((width,height),DOUBLEBUF)
    xaxis = width/1.5+140
    yaxis = height/2
    scale = 400
    iterations = 50

    for iy in range(height/2+1):
        for ix in range(width):
            z = 0+0j
            c = complex(float(ix-xaxis)/scale, float(iy-yaxis)/scale)
            if not(q*(q+(x-0.25))<y2/4.0 or (x+1.0)**2 + y2 <0.0625):
                 for i in range(iterations):
                     z = z**2+c
                     if abs(z) > 2:
                        v = 765*i/iterations
                        if v > 510:
                            color = (255, 255, v%255)
                        elif v > 255:
                            color = (255, v%255, 0)
                            color = (v%255, 0, 0)
                        color = (0, 0, 0)

            screen.set_at((ix, iy), color)
            screen.set_at((ix, height-iy), color)


    while True:
        event = pygame.event.poll()
        if (event.type == QUIT or
            (event.type == KEYDOWN and event.key == K_ESCAPE)):

if __name__ == "__main__":

[welcomewikilite wikiurl=”” sections=”Cardioid / bulb checking” settings=””]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.