我对矩阵进行 n 次运算,结果输出 n 个 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