- #1
StillAnotherDave
- 75
- 8
- Homework Statement
- Contour and Quiver Plots
- Relevant Equations
- Python
Hello all,
I have produced a contour plot of a given function f. To this, I have added the vector field (arrows) for the gradient of f calculated analytically. I have then also added the vector field numerically by using the np.gradient function in python.
In both cases, the vector field plots arrows that do not start on the contour lines of the original function. As a final task, I need to get the vectors to do so.
One idea is to divide the contour plot into grid segments. Then use the analytical code because the grid spacing will not be regular. But I think there might be different ways to do this. I'm a bit stuck, as my python isn't great. Any help would be appreciated.
My code is:
I have produced a contour plot of a given function f. To this, I have added the vector field (arrows) for the gradient of f calculated analytically. I have then also added the vector field numerically by using the np.gradient function in python.
In both cases, the vector field plots arrows that do not start on the contour lines of the original function. As a final task, I need to get the vectors to do so.
One idea is to divide the contour plot into grid segments. Then use the analytical code because the grid spacing will not be regular. But I think there might be different ways to do this. I'm a bit stuck, as my python isn't great. Any help would be appreciated.
My code is:
Code:
# contour plot
plt.figure(1)
ax1 = plt.axes()
w = np.arange(-2, 4.1, 0.1)
v = np.arange(-2, 3.1, 0.1)
[x, y] = np.meshgrid(w, v)
f = np.exp(-x**2-y**2) + 0.5*np.exp(-(x-1.5)**2 -2*y**2)
c = np.arange(0.1, 1.0, 0.1)
l = np.arange(0.1, 1, 0.2)
ax1.contour(x, y, f, levels=l, linewidths=0.5)
ax1.set_aspect('equal')
# quiver plot
w = np.arange(-2, 4.1, 1.0)
v = np.arange(-2, 3.1, 1.0)
[x1, y1] = np.meshgrid(w, v)
gradhi = (-2*x1*np.exp(-x1**2-y1**2))-(x1-1.5)*np.exp(-x1**2+3*x1-2.25-2*y1**2)
gradhj = -2*y1*np.exp(-x1**2-y1**2)-2*y1*np.exp(-x1**2+3*x1-2.25-2*y1**2)
ax1.quiver(x1, y1, gradhi, gradhj, width=0.003, angles='xy',\
scale_units='xy', scale=1)