A_Mlt
A_Mlt

Reputation: 1

Plotting arrows of arctan2 argument over a map in Python

I plot a matrix 'diff' (quadratique sum of the input matrix diff_X and diff_Y) as a colored map using imshow and I would like to display arrows showing the corresponding angle arctan2(diff_Y/diff_X) for each coordinates. This is my program so far:

import numpy as np 
import matplotlib.pyplot as plt 
import math 

#Data
N=5
diff_X=np.array([[-0.000125  , -0.00013   , -0.000125  , -0.00012308, -0.00012692],
       [-0.00012615, -0.000125  , -0.00013154, -0.00012308, -0.00012   ],
       [-0.00012423, -0.00011808, -0.00011577, -0.00012769, -0.00012923],
       [-0.00012115, -0.00012308, -0.00011962, -0.00012538, -0.00012308],
       [-0.00012808, -0.00013077, -0.00012808, -0.00012038, -0.00012885]])
diff_Y=np.array([[5.69230769e-05, 6.19230769e-05, 6.76923077e-05, 5.88461538e-05,
        6.34615385e-05],
       [5.73076923e-05, 6.92307692e-05, 6.19230769e-05, 6.61538462e-05,
        6.34615385e-05],
       [5.53846154e-05, 7.30769231e-05, 6.50000000e-05, 6.92307692e-05,
        6.23076923e-05],
       [6.57692308e-05, 5.57692308e-05, 6.50000000e-05, 6.38461538e-05,
        6.76923077e-05],
       [6.38461538e-05, 6.57692308e-05, 6.80769231e-05, 6.53846154e-05,
        6.34615385e-05]])

#Computation of the matrix diff and the corresponding angles (arctan2)
for i in range(0,N):    
    for j in range(0,N):  
        diff[i][j] = math.sqrt(diff_X[i][j]**2+diff_Y[i][j]**2)
        arrow_diff[i][j] = np.arctan2(diff_Y[i][j],diff_X[i][j])    
        j=j+1
    i=i+1

#Plot of the map
fig, ax = plt.subplots()
im = ax.imshow(diff, interpolation='bicubic', 
               cmap=cm.inferno,
               origin='upper',
               vmax=diff.max(), vmin=diff.min()) 
               # vmax=sum_max, vmin=sum_min) 
X, Y = np.meshgrid(np.arange(0, N, 1), np.arange(0, N, 1))
U = np.cos(X*diff_X)
V = np.sin(Y*diff_Y)
Q = ax.quiver(X, Y, U, V, arrow_diff, units='width')
qk = ax.quiverkey(Q, X, Y, 0.2, labelpos='E',
                    coordinates='figure')

However, this is what I get: map

I can't figure out how to get the arrows pointing in the right direction for each coordinates and also how to adapt their size to be proportionnal to their argument arctan2(diff_Y[i][j],diff_X[i][j]).

I would expect all the arrows pointing in one direction and with a size more or less constant. I tried changing the arguments of quiver and quiverkey but it doesn't work.

Upvotes: 0

Views: 58

Answers (1)

Nick ODell
Nick ODell

Reputation: 25190

I would like to display arrows showing the corresponding angle arctan2(diff_Y/diff_X) for each coordinates.

You don't need to do that - matplotlib will do that for you. If I'm understanding right, you're taking diff_X and diff_Y, and converting them from Cartesian coordinates into polar coordinates, but matplotlib expects Cartesian coordinates for quiver plots. They're already in Cartesian coordinates, so you don't have to do anything.

By changing the U and V arguments, you can get a quiver plot like this:

Q = ax.quiver(X, Y, diff_X, diff_Y, arrow_diff, units='width')

plot

This looks visually reasonable, given the input (negative X, positive Y, X is much bigger than Y.)

You could also convert your polar form back into Cartesian using the standard formula for this:

U = np.cos(arrow_diff) * diff
V = np.sin(arrow_diff) * diff
Q = ax.quiver(X, Y, U, V, arrow_diff, units='width')

Then you can change arrow_diff, (which is in units of radians) to change the direction of the arrow. The length is controlled by diff.

Upvotes: 1

Related Questions