Reputation: 75
I have a states matrix of 3x4 and a direction matrix of 4x12.
states = 3x4 matrix
directions = 4x12 matrix
The direction matrix has on each column a number of actions: up,right,down,left. For example if we are in state[0,0]
and we would like to know where to go next, we check the directions matrix directions[:,0]
and depending on which row has the highest value we go like this:
-for row 0 we pick up
-for row 1 we pick right
-for row 2 we pick down
-for row 3 we pick left
Now I'm trying to use the plt.quiver
function of python to show arrows on the preferred directions, but I cannot find any useful resource. The only thing that I found and understood is this:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.quiver((0,0), (0,0), (1,0), (1,3), units = 'xy', scale = 1)
plt.axis('equal')
plt.xticks(range(-5,6))
plt.yticks(range(-5,6))
plt.grid()
plt.show()
which basically shows an arrow from (0,0) to (1,1) and another from (0,0) to (1,3), but I cannot think of a way of implementing what I want by updating this. Does anyone have any suggestions?
What I'm trying to do is something like this https://matplotlib.org/2.0.0/examples/pylab_examples/quiver_demo.html, the ("pivot='mid'; every third arrow; units='inches'")
example.
Upvotes: 1
Views: 433
Reputation: 339220
I guess the following example will do what you need.
import numpy as np; np.random.seed(1)
import matplotlib.pyplot as plt
states = np.random.randint(0,2,size=(3,4))
directions = np.round(np.random.rand(4,12), 2)
x,y = np.meshgrid(np.arange(states.shape[1]), np.arange(states.shape[0]))
dirstates = np.argmax(directions, axis=0).reshape(3,4)
dirx = np.sin(dirstates*np.pi/2.)
diry = np.cos(dirstates*np.pi/2.)
fig, ax = plt.subplots()
ax.quiver(x,y,dirx,diry)
ax.margins(0.2)
plt.show()
Upvotes: 1