1

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.

enter image description here

My code is below.

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()
Yosup Park
  • 39
  • 4

1 Answers1

1

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

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

To remedy this, you need to convert your vectors result_a[0], result_a[1] to arrays. Simple ways can be found in this answer. For example,

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

yields the following (with plt.legend(); plt.show()):

enter image description here

Community
  • 1
  • 1
wflynny
  • 18,065
  • 5
  • 46
  • 67