Plotting vector fields

First, though, we will generate a standard Gaussian random field so that we have a nice gradient:

# Generate a vector field with a gradient
from scipy.ndimage.filters import gaussian_filter
x = np.arange(0,10,0.5)
y = np.arange(0,10,0.5)
phi = gaussian_filter(np.random.uniform(size=(20,20)), sigma=5)
plt.subplot(141)
plt.imshow(phi, interpolation='none')
plt.title(r'$Phi$')
plt.subplot(142)
plt.imshow(np.gradient(phi)[0], interpolation='none')
plt.title(r'$partial_xPhi$')
plt.subplot(143)
plt.title(r'$partial_yPhi$')
plt.imshow(np.gradient(phi)[1], interpolation='none')
plt.subplot(144)
plt.title(r'$| abla Phi|$')
plt.imshow(np.linalg.norm(np.gradient(phi), axis=0), interpolation='none')
plt.gcf().set_size_inches(8,4)

Following is the output of the preceding code:

Let's take look at the components of the vector field from the preceding output which shows the Gaussian random fields. The first plot is called Phi. The next plot shows the variation in X, and the one after shows the variation in Y. As we can see, the variation in the X direction, the gradient, and the variation in the Y direction are different, as is the magnitude or total amount of variation.

The greatest amount of variation is shown in the X direction, and the most change is shown in the Y direction. We will take a look at how we can plot these two components together.

Since this is a vector, we are actually dealing with four-dimensional data. The following output shows the scalar, three-dimensional data of Phi:

# Vectors with quiver
plt.imshow(phi, extent=(0,10,0,10), interpolation='none', origin='lower')

We will get the following output:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset