Home > OS >  Plotting randomly selected vectors using quiver (python, matplotlib)
Plotting randomly selected vectors using quiver (python, matplotlib)

Time:12-03

I start with a given vector field defined in two dimensions. I want to randomly pick M points in a 1 by 1 square, and plot the vectors that correspond to the desired vector field at those randomly selected points, using quiver from matplotlib.pyplot

import numpy as np
from math import *
import sys
import matplotlib.pyplot as plt
import numpy.random as rn

L=1

def dipole(x,y): #This is the vector field
   Bx = 0 
   By = 0
   position = [0,0]
   moment = [0,1]
   R = (x-position[0])**2 (y-position[1])**2
   R = np.sqrt(R)
   A = 3*(moment[0]*(x-position[0]) moment[1]*(y-position[1]))/R**5
   Bx = A*(x-position[0])-moment[0]/R**3
   By = A*(y-position[1])-moment[1]/R**3
   return np.array([Bx,By])

#We now choose M random vectors in the square

xm = [] #array of random vector positions
vectors = [] #vectors at the random positions
M = int(1e1) #number of vectors

for i in range(M):
   random_point = [rn.uniform(-L,L),rn.uniform(-L,L)]
   xm.append(random_point)
   vectors.append([dipole(random_point[0],random_point[1]). [0],dipole(random_point[0],random_point[1])[1]])

xm = np.array(xm)
vectors = np.array(vectors)                         

plt.quiver(xm,vectors[:,0],vectors[:,1])

returns

ValueError: Argument V has a size 10 which does not match 20, the number of arrow positions

Writing

plt.quiver([xm[:,0],xm[:,1]],np.array(vectors[:,0]),np.array(vectors[:,1]))

instead does not help. Any suggestions? Thanks

CodePudding user response:

plt.quiver(xm[:,0], xm[:,1],vectors[:,0],vectors[:,1])
  • Related