Contour and Vector Field Plots

 

matplotlib has extensive tools for creating and annotating two dimensional contour plots and vector field plots. A contour plot is used to visualize two-dimensional scalar functions, such as the electric potential V (x;y) or elevations h(x;y) over some physical terrain. Vector field plots come in different varieties. There are field line plots, which in some contexts are called streamline plots, that show the direction of a vector field over some 2D (x;y) range. There are also quiver plots, which consist essentially of a 2D grid of arrows, that give the direction and magnitude of a vector field over some 2D (x;y) range.

Making a 2D grid of points
When plotting a function f (x) of a single variable, the first step is usually to create a one-dimensional x array of points, and then to evaluate and plot the function f (x) at those points, often drawing lines between the points to create a continuous curve. Similarly, when making a two dimensional plot, we usually need to make a two-dimensional x-y array of points, and then to evaluate and plot the function f (x;y), be it a scalar or vector function, at those points, perhaps with continuous curves to indicate the value of the function over the 2D surface.

Thus, instead of having a line of evenly spaced x points, we need a grid of evenly spaced x-y points. Fortunately, NumPy has a function np.meshgrid for doing just that. The procedure is first to make an xarray at even intervals over the range of x to be covered, and then to do the same for y. These two one-dimensional arrays are used as input to the np.meshgrid function, which makes a two-dimensional mesh. Here is how it works:
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(-1,1,5)
y=np.linspace(2,6,5)
X,Y=np.meshgrid(x,y)
print(X)
print(Y)
plt.plot(X,Y,'o')

output
[[-1.  -0.5  0.   0.5  1. ]
 [-1.  -0.5  0.   0.5  1. ]
 [-1.  -0.5  0.   0.5  1. ]
 [-1.  -0.5  0.   0.5  1. ]
 [-1.  -0.5  0.   0.5  1. ]]
[[2. 2. 2. 2. 2.]
 [3. 3. 3. 3. 3.]
 [4. 4. 4. 4. 4.]
 [5. 5. 5. 5. 5.]
 [6. 6. 6. 6. 6.]]


Contour plots 
The principal matplotlib routines for creating contour plots are contour and contourf. Sometimes you would like to make a contour plot of a function of two variables; other times you may wish to make a contour plot of some data you have. 

Contour plots of functions
Figure below  shows two  different contour plots. One is produced using using contourf. Both the plots use the same function, which is the sum of a pair of Gaussians, one positive and the other negative.contour and contourf draw contour lines and filled contours, respectively. Except as noted, function signatures and return values are the same for both versions. The coordinates of the values in Z. X and Y must both be 2-D with the same shape as Z (e.g. created via numpy.)


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm # color maps
import matplotlib


def pmgauss(x, y):
    r1 = (x-2)**2 + (y-1)**2
    r2 = (x-1)**2 + (y-2)**2
    return 2*np.exp(-0.5*r1) - 3*np.exp(-2*r2)

a, b = 4, 3

x = np.linspace(0, a, 60)
y = np.linspace(0, b, 45)

X, Y = np.meshgrid(x, y)
Z = pmgauss(X, Y)
plt.figure()
plt.contour(X, Y, Z, 8, colors='k')
plt.figure()
plt.contourf(X, Y, Z, 12, cmap=cm.gray, zorder=0)



Streamline plots
matplotlib can also make streamline plots, which are sometimes called field line plots. Stream plot is basically a type of 2D plot used majorly by physicists to show fluid flow and 2D field gradients .The basic function to create a stream plot in Matplotlib is:

ax.streamplot(x_grid, y_grid, x_vec, y_vec, density=spacing)

Here x_grid and y_grid are arrays of the x and y points.The x_vec and y_vec represent the stream velocity of each point present on the grid.The attribute density=spacing specify that how much close the streamlines are to be drawn together.

Creating stream plot 

Let’s start by creating a simple stream plot that contains streamlines on a 10 by 10 grid.All the streamlines are parallel and pointing towards the right.The code below creates the stream plot containing horizontal parallel lines pointing to the right:

# Import libraries
import numpy as np
import matplotlib.pyplot as plt

# Creating dataset
x = np.arange(0, 10)
y = np.arange(0, 10)

# Creating grids
X, Y = np.meshgrid(x, y)

# x-component to the right
u = np.ones((10, 10))

# y-component zero
v = np.zeros((10, 10))

fig = plt.figure(figsize = (12, 7))

# Plotting stream plot
plt.streamplot(X, Y, u, v, density = 0.5)

# show plot
plt.show()



Comments

Popular posts from this blog

Python For Engineers CST 445 KTU Open Elective Notes S7 - Dr Binu V P 9847390760

Plotting- Simple plots,setting limits,subplot, semilog, loglog plots

Model Question Paper CST 445 KTU Python For Engineers