2

I do n-operations on matrices which results in the output of n of 2x1 matrices. I append them to a list and want to plot as points with x,y coordinate. I found I need to convert them to a numpy.array so I finish with nx2 ndarray and want to plot [:0] and [:1]. When I print results everything seems to be correct when I try to plot I finish with blank figure and no points at all. I noticed that ndarrays are separated by space not by comma but not sure how to interpret it. My code looks like:

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

I expect normal plot of random numbers not a blank. I can't find how to plot having ndarrays in form of nx2 matrix

1 Answer 1

3

I'm not sure what kind of line style you are looking for, but you'll see something if you change the linestyle to:

linestyle='-'

Or, if you wish to see the data points marked with x, just remove the keyword linestyle:

pl.plot(np.array(XsavedArr[:,0]),np.array(XsavedArr[:,1]),
        'x-',label = 'Xsaved')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.