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, ...

Get matplotlib Plotting Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.