too many legend with array column data in matplotlib
<p>I try to plot simple rotation matrix result with list data. but My figure with result array have so many index as screen dump image. and the second plot is not exact with my attribute(line style, etc.) 
I guess that I do mistake array handling to plot but don't know what.
Any comments are welcome. Thanks in advance.</p>

<p><a href="http://i.stack.imgur.com/i9HAP.png" rel="nofollow"><img src="http://i.stack.imgur.com/i9HAP.png" alt="enter image description here"></a></p>

<p>My code is below. </p>

<pre><code>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()
</code></pre>

<p>The issue is that you are trying to plot the two rows of <code>result_a</code> as if they were 1-dimensional <code>np.ndarray</code>s, when in fact they are <code>np.matrix</code> which are always 2-dimensional.  See for yourself:</p>

<pre><code>&gt;&gt;&gt; result_a[0].shape
(1, 19)
</code></pre>

<p>To remedy this, you need to convert your vectors <code>result_a[0], result_a[1]</code> to arrays.  Simple ways can be found <a href="http://stackoverflow.com/questions/3337301/numpy-matrix-to-array">in this answer</a>.  For example,</p>

<pre><code>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')
</code></pre>

<p>yields the following (with <code>plt.legend(); plt.show()</code>):</p>

<p><a href="http://i.stack.imgur.com/eWsTc.png" rel="nofollow"><img src="http://i.stack.imgur.com/eWsTc.png" alt="enter image description here"></a></p>

python arrays matplotlib legend