O'Reilly logo

matplotlib Plotting Cookbook by Alexandre Devert

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Visualizing the content of a 2D array

Let's start with the most basic scenario. We have a 2D array, and we want to visualize its content. As an example, we will visualize the Mandelbrot set. The Mandelbrot set, a famous fractal shape, associates a number of iterations to each point on the plane.

How to do it...

We will first fill a 2D square array with values and then call pyplot.imshow() to visualize it, as shown in the following code:

import numpy as np import matplotlib.cm as cm from matplotlib import pyplot as plt def iter_count(C, max_iter): X = C for n in range(max_iter): if abs(X) > 2.: return n X = X ** 2 + C return max_iter N = 512 max_iter = 64 xmin, xmax, ymin, ymax = -2.2, .8, -1.5, 1.5 X = np.linspace(xmin, xmax, N) Y = np.linspace(ymin, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required