3

嗨,我需要绘制一个矩阵的内容,其中每一行代表一个不同的特征,每一列是一个不同的时间点。换句话说,我想看到特征随时间的变化,我以矩阵的形式堆叠了每个特征。C是矩阵

A=C.tolist() #convert matrix to list.
R=[]
for i in xrange(len(A[0])):
    R+=[[i]*len(A[i])]    
for j in xrange(len(A[0])):
    S=[]
    S=C[0:len(C)][j]
    pylab.plot(R[j],S,'r*')
pylab.show()

这是正确的/有没有更有效的方法来做到这一点?谢谢!

4

2 回答 2

9

文档

matplotlib.pyplot.plot(*args, **kwargs):

[...]

plot(y)            # plot y using x as index array 0..N-1
plot(y, 'r+')      # ditto, but with red plusses

如果 x 和/或 y 是二维的,则将绘制相应的列。

因此,如果A列中有值,则很简单:

pylab.plot(A, 'r*')  # making all red might be confusing, '*-' might be better

如果您的数据在行中,则绘制它的转置:

pylab.plot(A.T, 'r*')
于 2012-08-24T01:10:44.270 回答
5

您可以提取矩阵 M 的第 iM[:,i]列,并且 M 中的列数由 给出M.shape[1]

import matplotlib.pyplot as plt

T = range(M.shape[0])

for i in range(M.shape[1]):
    plt.plot(T, M[:,i])

plt.show()

这假设行表示等距的时间片。

于 2012-08-24T00:34:25.920 回答