Python matplotlib中包含数组列数据的图例太多

Python matplotlib中包含数组列数据的图例太多,python,arrays,matplotlib,legend,Python,Arrays,Matplotlib,Legend,我尝试用列表数据绘制简单的旋转矩阵结果。但我的数字和结果数组有很多索引,就像屏幕转储图像一样。第二个图与我的属性(线条样式等)不完全一致 我猜我确实把数组处理错了,但我不知道是什么。 欢迎提出任何意见。提前谢谢 我的代码如下 import numpy as np import matplotlib.pyplot as plt theta = np.radians(30) c, s = np.cos(theta), np.sin(theta) R = np.matrix('{} {}; {}

我尝试用列表数据绘制简单的旋转矩阵结果。但我的数字和结果数组有很多索引,就像屏幕转储图像一样。第二个图与我的属性(线条样式等)不完全一致 我猜我确实把数组处理错了,但我不知道是什么。 欢迎提出任何意见。提前谢谢

我的代码如下

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

问题是,您试图绘制两行
result\u a
,就好像它们是一维
np.ndarray
s,而实际上它们是
np.matrix
,始终是二维的。你自己看看:

>>> result_a[0].shape
(1, 19)
要解决这个问题,您需要将向量
result\u a[0],result\u a[1]
转换为数组。可以找到简单的方法。比如说,

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


也许结果a没有您期望的形状?是的,问题是数据类型。非常感谢。