.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_intro_numpy_auto_examples_plot_mandelbrot.py: Mandelbrot set ============== Compute the Mandelbrot fractal and plot it .. image:: /intro/numpy/auto_examples/images/sphx_glr_plot_mandelbrot_001.png :class: sphx-glr-single-img .. code-block:: python import numpy as np import matplotlib.pyplot as plt from numpy import newaxis def compute_mandelbrot(N_max, some_threshold, nx, ny): # A grid of c-values x = np.linspace(-2, 1, nx) y = np.linspace(-1.5, 1.5, ny) c = x[:,newaxis] + 1j*y[newaxis,:] # Mandelbrot iteration z = c # The code below overflows in many regions of the x-y grid, suppress # warnings temporarily with np.warnings.catch_warnings(): np.warnings.simplefilter("ignore") for j in range(N_max): z = z**2 + c mandelbrot_set = (abs(z) < some_threshold) return mandelbrot_set mandelbrot_set = compute_mandelbrot(50, 50., 601, 401) plt.imshow(mandelbrot_set.T, extent=[-2, 1, -1.5, 1.5]) plt.gray() plt.show() **Total running time of the script:** ( 0 minutes 0.106 seconds) .. _sphx_glr_download_intro_numpy_auto_examples_plot_mandelbrot.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: plot_mandelbrot.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: plot_mandelbrot.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_