2017-05-05 6 views
1

私は3x4の状態マトリックスと4x12の方向マトリックスを持っています。アップ、右、下、左:plt.quiverを制御するには?

states = 3x4 matrix 
directions = 4x12 matrix 

方向行列は、各列のアクションの数を有しています。今、私がしようとしている

-for row 0 we pick up 
-for row 1 we pick right 
-for row 2 we pick down 
-for row 3 we pick left 

:我々はstate[0,0]であり、私たちはどこ次に行き知っていただきたいと思います、我々は方向行列directions[:,0]をチェックし、依存している場合たとえば、行は、私たちはこのように行くの最高値を持ちます、 Pythonのplt.quiver関数を使用して、優先方向に矢印を表示することはできますが、役に立つリソースは見つかりません。 、基本的に(0,0)乃至(0,0)から矢印(1,1)と別の(1,3)を示す

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() 

:私は発見し理解唯一のものはこれですしかし、私はこれを更新して、私が望むものを実装する方法を考えることはできません。 誰にも提案はありますか?

私がしようとしているのは、https://matplotlib.org/2.0.0/examples/pylab_examples/quiver_demo.html("pivot='mid'; every third arrow; units='inches'")のようなものです。

答えて

1

私は次の例が必要なことをすると思います。

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() 

enter image description here

+0

母、素敵な1。私がやったのは 'X = np.array([0,1,2,3,0,1,2,3,0,1,2,3])' 'Y = np.array([2 、2,2,2,1,1,1,1,0,0,0,0])、方向についてはargmaxをとり、Uについては、VIは方向を0,1または-1,1。あなたのソリューションはとてもエレガントです。私の宇宙は二倍です。 – iDaniel19

関連する問題