Getting vector arrows to start on contour lines

  • Comp Sci
  • Thread starter StillAnotherDave
  • Start date
  • Tags
    Lines Vector
In summary, the speaker discusses their process of creating a contour plot and adding vector fields to it using both analytical and numerical methods. They mention that the vector field arrows do not start on the contour lines and are seeking help to fix this issue. They suggest using grid segments and mention their limited knowledge of Python. They also provide their code for reference.
  • #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:
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)
 
Physics news on Phys.org
  • #2
# numerical gradientX, Y = np.meshgrid(w, v)F = np.exp(-X**2-Y**2) + 0.5*np.exp(-(X-1.5)**2 -2*Y**2)dF_dx, dF_dy = np.gradient(F, 1.0)ax1.quiver(X, Y, dF_dx, dF_dy, width=0.003, angles='xy',\ scale_units='xy', scale=1)plt.show()Thanks in advance!
 

FAQ: Getting vector arrows to start on contour lines

How do you get vector arrows to start on contour lines?

To get vector arrows to start on contour lines, you will need to use a GIS software or tool that allows you to customize the appearance of vector arrows. In most cases, you can simply adjust the settings or properties of the vector arrows to align them with the contour lines.

Can you manually adjust the position of vector arrows to start on contour lines?

Yes, most GIS software or tools have the option to manually adjust the position of vector arrows. This allows you to precisely align the arrows with the contour lines.

Are there any specific techniques or methods for getting vector arrows to start on contour lines?

There are a few techniques that can help you get vector arrows to start on contour lines. One method is to use a snapping tool to snap the arrows to the contour lines. Another technique is to use a buffer tool to create a buffer around the contour lines and then use this buffer to guide the placement of the vector arrows.

What is the importance of having vector arrows start on contour lines?

Having vector arrows start on contour lines is important because it helps to accurately represent the direction and magnitude of the data being displayed. This allows for better interpretation and analysis of the data.

Are there any limitations or challenges when trying to get vector arrows to start on contour lines?

There can be limitations or challenges when trying to get vector arrows to start on contour lines, especially when dealing with complex or irregularly shaped contour lines. In some cases, the arrows may not align perfectly with the contour lines, but with careful adjustment and use of techniques, you can still achieve a relatively accurate representation of the data.

Similar threads

Back
Top