2016-09-14 10 views
-1

リストデータを使って単純な回転行列の結果をプロットしようとします。結果配列のある図は画面ダンプイメージと同じくらい多くのインデックスを持っています。 2番目のプロットは私のアトリビュート(ラインスタイルなど)と正確ではありません 私はプロットに間違った配列処理をしていますが、何がわからないのでしょうか。 コメントは歓迎します。前もって感謝します。matplotlibの配列の列データを持つ凡例が多すぎます

enter image description here

私のコードは以下の通りです。

import numpy as np 
import matplotlib.pyplot as plt 

theta = np.radians(30) 
c, s = np.cos(theta), np.sin(theta) 
R = np.matrix('{} {}; {} {}'.format(c, -s, s, c)) 
x = [-9, -8, -7, -6, -5, -4, -3, -2, -1,0,1,2,3,4,5,6,7,8,9] 
y = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] 

line_b = [x,y] 

result_a = R*np.array(line_b) 

fig=plt.figure() 
ax1 = fig.add_subplot(111) 
plt.plot(line_b[0],line_b[1], color="blue", linewidth=2.5, linestyle="-", label='measured') 
plt.plot(result_a[0], result_a[1], 'r*-', label='rotated') 
ax1.set_ylim(-10,10) 
ax1.set_xlim(-10,10) 
plt.legend() 

# axis center to move 0,0 
ax1.spines['right'].set_color('none') 
ax1.spines['top'].set_color('none') 
ax1.xaxis.set_ticks_position('bottom') 
ax1.spines['bottom'].set_position(('data',0)) 
ax1.yaxis.set_ticks_position('left') 
ax1.spines['left'].set_position(('data',0)) 

plt.show() 
+1

おそらくresult_aは、あなたがそれを持っていることを期待する形状を有していないのですか? – CAB

答えて

0

問題は、実際に彼らは常に2次元であるnp.matrixているときに、彼らは1次元np.ndarray Sであるかのようresult_aの2つの行をプロットしようとしているということです。

>>> result_a[0].shape 
(1, 19) 

これを解決するには、ベクトルresult_a[0], result_a[1]を配列に変換する必要があります。簡単な方法はin this answerです。例えば、

rx = result_a[0].A1 
ry = result_a[1].A1 
# alternatively, the more compact 
# rx, ry = np.array(result_a) 
plt.plot(rx, ry, 'r*-', label='rotated') 

は(plt.legend(); plt.show()で)以下が得られます。

enter image description here

+0

はい、問題はデータ型です。どうもありがとう。 –

関連する問題