Session Ready
Exercise

Generating meshes

In order to visualize two-dimensional arrays of data, it is necessary to understand how to generate and manipulate 2-D arrays. Many Matplotlib plots support arrays as input and in particular, they support NumPy arrays. The NumPy library is the most widely-supported means for supporting numeric arrays in Python.

In this exercise, you will use the meshgrid function in NumPy to generate 2-D arrays which you will then visualize using plt.imshow(). The simplest way to generate a meshgrid is as follows:

import numpy as np
Y,X = np.meshgrid(range(10),range(20))

This will create two arrays with a shape of (20,10), which corresponds to 20 rows along the Y-axis and 10 columns along the X-axis. In this exercise, you will use np.meshgrid() to generate a regular 2-D sampling of a mathematical function.

Instructions
100 XP
  • Import the numpy and matplotlib.pyplot modules using the respective aliases np and plt.
  • Generate two one-dimensional arrays u and v using np.linspace(). The array u should contain 41 values uniformly spaced between -2 and +2. The array v should contain 21 values uniformly spaced between -1 and +1.
  • Construct two two-dimensional arrays X and Y from u and v using np.meshgrid().
  • After the array Z is computed using X and Y, visualize the array Z using plt.pcolor() and plt.show().
  • Save the resulting figure as 'sine_mesh.png'.