One Python code line for a Mandelbrot fractal

Create a Mandelbrot or Buddhabrot fractal in Python using NeuroKit.

A Mandelbrot fractal made with Python using NeuroKit

Mandelbrot Set

I wrote a small Python function to easily generate and plot a Mandelbrot set. This function is now available through the NeuroKit2 package, and can be used as follows:

import neurokit2 as nk

nk.fractal_mandelbrot(show=True)

The Mandelbrot set is defined in the between -2 and 2 on the x (real) and y (imaginary) axes. Following that, the image can be cropped accodingly by changing the coordinates. Moreover, the colors can be tweaked by changing the the colormap (cmap).

m = nk.fractal_mandelbrot(real_range=(-2, 0.75), imaginary_range=(-1.25, 1.25))
plt.imshow(m.T, cmap="viridis")
plt.axis("off")
plt.show()

Buddhabrot Set

It is also possible to generate a Buddhabrot:

b = nk.fractal_mandelbrot(size=1500,
                          real_range=(-2, 0.75), imaginary_range=(-1.25, 1.25),
                          buddha=True, iterations=200)
plt.imshow(b.T, cmap="gray")
plt.axis("off")
plt.show()

Althoug the NeuroKit Python package is primarily devoted at physiological signal processing, in also includes tons of other useful features.

👉 Discover more about NeuroKit here 👈

Have fun!


Thanks for reading! Do not hesitate to tweet and share this post, and leave a comment below 🤗

🐦 Don’t forget to join me on Twitter @Dom_Makowski

Dominique Makowski
Dominique Makowski
Lecturer in Psychology

Trained as neuropsychologist, I am currently working as a lecturer at the University of Sussex, on the neuroscience of the perception of reality and how it relates to who we are.