Home > front end >  My arrows are too frequent and too small in quiver
My arrows are too frequent and too small in quiver

Time:11-12

I have a 100x100 unit vectorfield created on a meshgrid as follows. I evaluate some function on every point combination in the X and Y meshgrids.

import numpy as np
import matplotlib.pyplot as plt
N=100 # of points
#set arrays of n, m and td_50 to try
n_array=np.linspace(0.01,1,N)
td50_array=np.linspace(1,100,N)

X,Y=np.meshgrid(n_array,td50_array)

Z=X*Y

I am now trying to plot a vectorfield of the gradient of Z, so I do

#compute gradient,normalized gradient x and y components, as well as gradient raw magnitude
gradx,grady=np.gradient(Z)
gradxnorm=gradx/np.sqrt(gradx**2 grady**2)
gradynorm=grady/np.sqrt(gradx**2 grady**2)
gradmag=np.sqrt(gradx**2 grady**2)
#plot
plt.pcolor(X, Y, gradmag,cmap='rainbow')
plt.colorbar()
plt.quiver(X, Y, gradxnorm , gradynorm,units='xy')
plt.show()

And I want to make a plot of the gradient magnitude overlay with the direction of the gradient (with uniform values)

If I set N=10, then I get a nice clean plot that looks like this. This is exactly what I want enter image description here

However, I want to evaluate at more than just 10 points in each direction, but if I set N=100, I get a plot like this instead where the arrows are far too small and far too frequent to be useful overlayed with the gradient.

enter image description here

I can see that this is just because I have many more points, but is it possible to make the arrows on this second plot look more like the arrows on the first plot? I don't need an arrow for every single point in the grid, so I would like fewer arrows that are actually larger instead of one tiny arrow at every single pixel.

CodePudding user response:

When you use plt.quiver you might want to plot the arrows every N datapoints instead of every datapoints. This requires indexing your X, Y,gradxnorm and gradynorm accordingly. Below is an example where I only plot points with x and y coordinates multiples of 10:

import numpy as np
import matplotlib.pyplot as plt
N=100 # of points
#set arrays of n, m and td_50 to try
n_array=np.linspace(0.01,1,N)
td50_array=np.linspace(1,100,N)

X,Y=np.meshgrid(n_array,td50_array)

Z=X*Y

#compute gradient,normalized gradient x and y components, as well as gradient raw magnitude
gradx,grady=np.gradient(Z)
gradxnorm=gradx/np.sqrt(gradx**2 grady**2)
gradynorm=grady/np.sqrt(gradx**2 grady**2)
gradmag=np.sqrt(gradx**2 grady**2)
step=10
plt.pcolor(X, Y, gradmag,cmap='rainbow')
plt.colorbar()

plt.quiver(X[::step,::step], Y[::step,::step],gradxnorm[::step,::step] , gradynorm[::step,::step],units='xy')
plt.show()

The output gives:

enter image description here

You can modify the variable step to increase or decrease the number of arrows.

  • Related