.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "intro/numpy/auto_examples/plot_mandelbrot.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_intro_numpy_auto_examples_plot_mandelbrot.py>`
        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

.. GENERATED FROM PYTHON SOURCE LINES 8-42



.. image-sg:: /intro/numpy/auto_examples/images/sphx_glr_plot_mandelbrot_001.png
   :alt: plot mandelbrot
   :srcset: /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
    import warnings


    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 warnings.catch_warnings():
            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.0, 601, 401)

    plt.imshow(mandelbrot_set.T, extent=(-2, 1, -1.5, 1.5))
    plt.gray()
    plt.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.076 seconds)


.. _sphx_glr_download_intro_numpy_auto_examples_plot_mandelbrot.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_mandelbrot.ipynb <plot_mandelbrot.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_mandelbrot.py <plot_mandelbrot.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_mandelbrot.zip <plot_mandelbrot.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_