Understanding the Mandelbrot Set, the Julia Set, and Their Relationship

This little article is more for my own notes. Hoping to add more details later.

What is the Mandelbrot Set?

What is the Julia Set?

Relationship Between the Mandelbrot Set and the Julia Set

Python Implementation to Display a Mandelbrot Set

Here’s a simple Python implementation to display a Mandelbrot set:

import numpy as np
import matplotlib.pyplot as plt

def mandelbrot(c, max_iter):
    z = c
    for n in range(max_iter):
        if abs(z) > 2:
            return n
        z = z*z + c
    return max_iter

def draw_mandelbrot(xmin,xmax,ymin,ymax,width,height,max_iter):
    r1 = np.linspace(xmin, xmax, width)
    r2 = np.linspace(ymin, ymax, height)
    return (r1,r2,np.array([[mandelbrot(complex(r, i),max_iter) for r in r1] for i in r2]))

def main():
    dpi = 80
    img_width = dpi * 3
    img_height = dpi * 3
    xmin = -2.0
    xmax = 1.0
    ymin = -1.5
    ymax = 1.5
    max_iter = 256

    x,y,z = draw_mandelbrot(xmin,xmax,ymin,ymax,img_width,img_height,max_iter)

    fig, ax = plt.subplots(figsize=(3,3),dpi=dpi)
    ticks = np.arange(0,img_width,3*dpi)
    x_ticks = xmin + (xmax-xmin)*ticks/img_width
    plt.xticks(ticks, x_ticks)
    y_ticks = ymin + (ymax-ymin)*ticks/img_width
    plt.yticks(ticks, y_ticks)
    ax.set_title("Mandelbrot Set")
    ax.imshow(z, origin='lower', cmap='hot')
    plt.show()

if __name__ == "__main__":
    main()

This script generates an image of the Mandelbrot set. The mandelbrot function determines the iteration count at which a point escapes to infinity (or reaches the maximum iteration count). The draw_mandelbrot function generates the Mandelbrot set over a given range and resolution. The main function sets up the plot, calls draw_mandelbrot to generate the data, and then displays the result.

Please note that this is a basic implementation and there are many ways to optimize and enhance this code to generate more detailed or colorful images of the Mandelbrot set. I’ve got more pictures to upload soon. Documenting this for fun at the moment.

I hope this article helps you understand the Mandelbrot set, the Julia set, and their relationship. Happy coding!