密谋numpy.ndarray

时间:2012-10-23 15:45:44

标签: python numpy

我对矩阵进行n次运算,这导致nx的2x1矩阵的输出。我将它们附加到列表中,并希望绘制为具有x,y坐标的点。我发现我需要将它们转换为numpy.array,所以我用nx2 ndarray完成并想要绘制[:0][:1]。当我打印结果时,一切似乎是正确的,当我尝试绘制我完成空白图并且根本没有点。我注意到ndarrays是用空格分隔的,而不是用逗号分隔,但不确定如何解释它。我的代码如下:

import numpy as np
import pylab as pl

class GetMat(object):
    def __init__(self):
        self.Posp = np.matrix([[1,1],[1,1]])
        self.Velp = np.matrix([[1],[1]])
    def __call__(self):
        self.Posp = self.Posp*np.random.random()
        self.Velp = self.Velp*np.random.random()
        return self.Posp,self.Velp
    def __repr__(self):
        return "Posp = \n%s \nVelp = \n%s" % (self.Posp,self.Velp)
class Data(object):
    def __init__(self):
        self.x = 0
    def __call__(self,P,V):
        self.x = P*V
        return self.x[0,0], self.x[1,0]
    def __repr__(self):
        return "x = %s" % self.x

def Test():
    Nsamples = np.arange(0,10,1)
    newMat = GetMat()
    dataToPlot = Data()
    Xsaved = []
    for i in range(len(Nsamples)):
        z1,z2 = newMat()
        position, velocity = dataToPlot(z1,z2)
        Xsaved.append([position, velocity])
    print 'Xsaved = %s' % Xsaved
    print 'tyep Xsaved %s' % type(Xsaved)
    XsavedArr = np.array(Xsaved)
    print 'XsavedArr %s' % XsavedArr
    print 'tyep XsavedArr %s' % type(XsavedArr)
    print 'XsavedArr[:,0] = %s' % XsavedArr[:,0]
    print 'XsavedArr[:,1] = %s' % XsavedArr[:,1]
    pl.plot(np.array(XsavedArr[:,0]),np.array(XsavedArr[:,1]), linestyle='x-',label = 'Xsaved')
    pl.show()

if __name__ == '__main__':
    Test()      

我预计随机数的正常情节不是空白。我找不到如何以nx2矩阵的形式绘制ndarray

1 个答案:

答案 0 :(得分:3)

我不确定您要寻找的是什么样的线条样式,但如果您将线条样式更改为:

,您会看到某些内容
linestyle='-'

或者,如果您希望查看标有x的数据点, 只需删除关键字linestyle

即可
pl.plot(np.array(XsavedArr[:,0]),np.array(XsavedArr[:,1]),
        'x-',label = 'Xsaved')