说
twenty = [[0.00186157 0.00201416 0.00216675 0.00213623 0.00253296 0.00250244 0.00280762 0.00292969 0.00308228 0.0032959 0.00338745 0.003479 0.003479 0.00341797 0.00335693 0.00320435 0.00308228 0.0027771 0.00253296 0.00216675]]
twentyfirst = [[0.00186157]]
以下功能-虽然它应同时针对散点图和线图进行绘制,(这与page中的实现完全相同)可以同时在标记中绘制,但matplotlib
在生成行中迷失了。
def plot_time_series(twenty, twentyfirst):
xlabel = np.arange(0, 1, 1./20).reshape(1,20)
print(np.ones(twenty.shape[1])[np.newaxis,:].shape) #(1,20)
A = np.vstack([xlabel, np.ones(twenty.shape[1])[np.newaxis,:]]).T
m, c = np.linalg.lstsq(A, twenty.T)[0]
print(m, c)
plt.scatter(xlabel, twenty.T, c='b', label='data')
ylabel = m*xlabel + c
print(ylabel.shape) #(1,20)
plt.plot(xlabel, ylabel, '-ok', label = 'fitted line')
plt.legend(loc='best')
plt.ylabel('amplitudes')
plt.savefig('timeseries_problem2'+'_4')
plt.close()
答案 0 :(得分:1)
在引擎盖下,这个问题询问绘图之间的区别
plt.plot([[1,2,3]],[[2,3,1]])
和
plt.plot([[1],[2],[3]],[[2],[3],[1]])
在两种情况下,列表都是二维的。在第一种情况下,您只有一行数据。在第二种情况下,您只有一列数据。
x
,y
:类数组或标量
[...]
通常,这些参数是长度为N的数组。但是,也支持标量(相当于具有常数值的数组)。参数也可以是二维。然后,列代表单独的数据集。
重要的部分是最后一句话。如果数据是2D(如此处所示),则按列进行解释。由于行数组[[2,3,1]]
由3列组成,每列都有一个值。 plot
因此将产生1点的3条单“线”。但是由于单个点没有定义线,因此只有在激活标记(例如
plt.plot([[1,2,3]], [[2,3,1]], marker="o")
将此行数组转换为列数组时,将被解释为具有3个条目的单个数据集。因此,单行的预期结果
plt.plot([[1],[2],[3]], [[2],[3],[1]])
当然也可以将数组展平为一维,
plt.plot(np.array([[1,2,3]]).flatten(), np.array([[2,3,1]]).flatten())
您可以轻松地查看您产生了多少行
print(len(plt.plot([[1,2,3]],[[2,3,1]]))) # prints 3
print(len(plt.plot([[1],[2],[3]],[[2],[3],[1]]))) # prints 1