## Plotting Vector Fields 

In this note, we will build a couple of different vector-field plots using `Quiver` library of `Matplotlib`.

**Reference**
 1. [Quiver plots](https://pythonforundergradengineers.com/quiver-plot-with-matplotlib-and-jupyter-notebooks.html)


```python
# Math and plotting libraries:
#import sympy
#from sympy.physics.vector import *
#from sympy import Curve, line_integrate, E, ln, diff
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
```

The `quiver()` method takes four positional arguments:

> `quiver(x_pos, y_pos, x_direct, y_direct)`

where `x_pos` & `y_pos` are the arrow starting positions and `x_direct` & `y_direct` 
are the arrow directions.

**Example 1** Plotting the vector field $\vec{F}(x,y) = \vec{i}-\vec{j}$, where
$\vec{i} = <1, 0>$ and $\vec{j} = <0, 1>$ are standard unit vecotrs in 2D.


```python
# Establish coordinates for the arrow starting positions as a set of arrays
x,y = np.meshgrid(np.linspace(-5,5,10), np.linspace(-5,5,10)) # 10x10 = 100 positions

# Define the quiver arrow direction arrays in a vector field format.
u = 1
v = -1

# Plot the vector field
plt.figure(figsize=(7,7))

plt.quiver(x,y,u,v)

plt.xlabel('x')
plt.ylabel('y')
plt.show()
```

**Example 2** Plotting the vector field $\vec{F}(x,y) = <\cos(x), \sin(y)>$.


```python
x,y = np.meshgrid(np.arange(0,2.2,0.2), np.arange(0,2.2,0.2)) # 10x10 = 100 positions

u = np.cos(x)*y
v = np.sin(y)*y

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

plt.quiver(x,y,u,v)

plt.xlabel('x')
plt.ylabel('y')
plt.title("$F=<\cos(x),\sin(y)>$")
plt.show()
```

## Plotting Gradient Vector Fields

Next, let's build a quiver plot of the gradient vector $\vec{\nabla} f$ of a scalar-valued function $f(x,y)$. We will use numpy's `np.gradient()` function to apply $\vec{\nabla} f$ based on every arrow's $x, y$.

**Example 3** Plotting the gradient vector field of $f(x,y) = xe^{-x^2-y^2}$.


```python
x,y = np.meshgrid(np.arange(-2,2.2,0.2), np.arange(-2,2.2,0.2)) 

z = x*np.exp(-x**2-y**2)
dx, dy = np.gradient(z)

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

plt.quiver(x,y,dx,dy)

#plt.xticks([])
#plt.yticks([])
plt.title("Gradient vector Field of $f(x,y) = xe^{-x^2-y^2}$")
plt.show()
```

Next, let's add some color to the quiver plots. The `quiver()` function has an optional fifth positional argument that specifies the quiver arrow color. The quiver arrow color argument needs to have the same dimensions as the position and direction arrays.

**Example 4** Let's add some colors to the gradient vector plot in Example 3.


```python
x,y = np.meshgrid(np.arange(-2,2.2,0.2), np.arange(-2,2.2,0.2)) 

z = x*np.exp(-x**2-y**2)
dx, dy = np.gradient(z)

n = -2
color_array = np.sqrt(((dx-n)/2)**2 + ((dy-n)/2)**2)

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

plt.quiver(x,y,dx,dy, color_array)

#plt.xticks([])
#plt.yticks([])
plt.title("Gradient vector Field of $f(x,y) = xe^{-x^2-y^2}$")
plt.show()
```

## 3D Vector Fields


```python
%reset -f # Reset all previously defined variables
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np

%matplotlib notebook # To generate interactive plot

fig = plt.figure(figsize=(8,8))
ax = fig.gca(projection='3d')

x, y, z = np.meshgrid(np.arange(-0.8, 1, 0.2),
                      np.arange(-0.8, 1, 0.2),
                      np.arange(-0.8, 1, 0.2))

u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) *
     np.sin(np.pi * z))

ax.quiver(x, y, z, u, v, w, length=0.1)

ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

#plt.show()
```


    <IPython.core.display.Javascript object>









    Text(0.5, 0, 'Z axis')



## Exercises

1. Plot the vector fields:

   (a) $\vec{F}(x,y) = \frac{x}{\sqrt{x^2+y^2}}\vec{i} + \frac{y}{\sqrt{x^2+y^2}}\vec{j}$
   
   (b) $\vec{F}(x,y) = -\frac{y}{\sqrt{x^2+y^2}}\vec{i} + \frac{x}{\sqrt{x^2+y^2}}\vec{j}$
   
   (c) $\vec{F}(x,y) = \sin(x)\cos(y)\vec{i} - \cos(x)\sin(y)\vec{j}$


```python

```

0c035a1a25b05c472e5987ae5eee8434e4c62223
ipynb
Jupyter Notebook
T2_2_Plotting_Vector_Fields_with_Quiver.ipynb
bkimo/Multivariable_Calculus_with_Python
a6225d47d7246557efb54caeaeecfa55c9560856
MIT
T2_2_Plotting_Vector_Fields_with_Quiver.ipynb
bkimo/Multivariable_Calculus_with_Python
a6225d47d7246557efb54caeaeecfa55c9560856
MIT
T2_2_Plotting_Vector_Fields_with_Quiver.ipynb
bkimo/Multivariable_Calculus_with_Python
a6225d47d7246557efb54caeaeecfa55c9560856
MIT
Qwen/Qwen-72B
1. YES
2. YES