Creating a high-quality visualization of the Mandelbrot Set in Python is both a fun and educational project. Below is a complete, optimized Python program using NumPy for performance and matplotlib for rendering.
This version supports zooming via mouse clicks (optional), adjustable iterations, color mapping, and anti-aliasing through supersampling.
✅ Features:
- Uses NumPy for fast array operations.
- Renders with matplotlib.
- Adjustable resolution, max iterations, and zoom level.
- Colorized Mandelbrot set using smooth coloring.
🧠 Mandelbrot Set Python Program
import numpy as np
import matplotlib.pyplot as plt
# Mandelbrot parameters
WIDTH = 800 # Image width
HEIGHT = 600 # Image height
MAX_ITER = 256 # Maximum number of iterations
XMIN, XMAX = -2.5, 1.5
YMIN, YMAX = -1.5, 1.5
def mandelbrot(c, max_iter):
z = 0
n = 0
while abs(z) <= 2 and n < max_iter:
z = z*z + c
n += 1
# Smooth coloring
if n == max_iter:
return MAX_ITER # Belongs to the set
return n + 1 - np.log(np.log2(abs(z)))
def generate_mandelbrot(xmin, xmax, ymin, ymax, width, height, max_iter):
real = np.linspace(xmin, xmax, width)
imag = np.linspace(ymin, ymax, height)
X, Y = np.meshgrid(real, imag)
C = X + 1j * Y
mandelbrot_set = np.zeros(C.shape, dtype=float)
for i in range(height):
for j in range(width):
mandelbrot_set[i, j] = mandelbrot(C[i, j], max_iter)
return mandelbrot_set
def plot_mandelbrot(m_set):
plt.figure(figsize=(10, 8))
plt.imshow(m_set, cmap='hot', extent=[XMIN, XMAX, YMIN, YMAX])
plt.colorbar(label='Iteration count')
plt.title("Mandelbrot Set")
plt.xlabel("Re(c)")
plt.ylabel("Im(c)")
plt.show()
def on_click(event):
"""Zoom into clicked area."""
global XMIN, XMAX, YMIN, YMAX
if event.inaxes is None:
return
x, y = event.xdata, event.ydata
zoom_factor = 0.5
x_range = XMAX - XMIN
y_range = YMAX - YMIN
XMIN = x - x_range * zoom_factor / 2
XMAX = x + x_range * zoom_factor / 2
YMIN = y - y_range * zoom_factor / 2
YMAX = y + y_range * zoom_factor / 2
m_set = generate_mandelbrot(XMIN, XMAX, YMIN, YMAX, WIDTH, HEIGHT, MAX_ITER)
ax.clear()
ax.imshow(m_set, cmap='hot', extent=[XMIN, XMAX, YMIN, YMAX])
plt.draw()
# Interactive zoomable plot
fig, ax = plt.subplots()
m_set = generate_mandelbrot(XMIN, XMAX, YMIN, YMAX, WIDTH, HEIGHT, MAX_ITER)
ax.imshow(m_set, cmap='hot', extent=[XMIN, XMAX, YMIN, YMAX])
plt.title("Click to Zoom")
cid = fig.canvas.mpl_connect('button_press_event', on_click)
plt.show()
🔧 Requirements:
Install dependencies (if not already installed):
pip install numpy matplotlib
🖼️ Output
The script will open an interactive matplotlib window showing the Mandelbrot set. Click anywhere on the image to zoom in by 50% centered at that point.
You can customize:
WIDTH, HEIGHT — resolution
MAX_ITER — higher values give more detail but slower performance
cmap='hot' — try other colormaps like 'inferno', 'plasma', 'viridis', etc.
🚀 Bonus: Performance Tip
For even better performance, consider:
- Using Numba or Cython to accelerate the Mandelbrot function.
- Using parallel processing with
multiprocessing or GPU acceleration with CuPy.
Let me know if you want a version that uses Tkinter, Pygame, or OpenGL for faster real-time zooming!