1

I am trying to make in python an array within an array that looks like this:

[[3,0,-3,-4],[6,0,-2.44,-4]]

which I could use in a vector plot in matplotlib.

I tried doing this with the following program:

data = np.loadtxt(sys.argv[1], dtype='str',delimiter=',', skiprows=1, usecols=(0,1,2,3,4,5,6,7))

x = data[:,0].astype(float)
u = data[:,6].astype(float)
v = data[:,7].astype(float)

soa = []
for t in range(0,2):
    print "At time ",x[t]," U is ",u[t]," and V is ",v[t]
    result = [x[t],0,u[t],v[t]] 
    soa = np.append(soa, [result])


print "soa is ",soa

When I run the program I get the output:

soa is [ 3. 0. -3. -4. 6. 0. -2.44 -4. ]

This cannot be plotted as a vector plot in matplotlib. How can I tweak the above script to get the array into the format:

 [[3,0,-3,-4],[6,0,-2.44,-4]]

where [3,0,-3,-4] and [6,0,-2.44,-4] are vectors that I could plot in matplotlib?

1
  • quick fix, soa = soa.reshape((2,-1)) Commented Feb 2, 2017 at 22:25

2 Answers 2

1

np.append is different than the noraml array append.

try:

for t in range(0,2):
    print "At time ",x[t]," U is ",u[t]," and V is ",v[t]
    result = [x[t],0,u[t],v[t]] 
    soa.append(result)
Sign up to request clarification or add additional context in comments.

Comments

1

The easiest way to get the desired array is to simply stack the columns from the data and put a zero column in the middle

data = np.loadtxt(sys.argv[1], dtype='str',delimiter=',', skiprows=1, 
                  usecols=(0,1,2,3,4,5,6,7))

x = data[:,0].astype(float)
u = data[:,6].astype(float)
v = data[:,7].astype(float)

soa = np.vstack((x,np.zeros(len(x)),u,v)).T

print "soa is ",soa

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.