我试图在python中创建一个如下所示的数组:
[[3,0,-3,-4],[6,0,-2.44,-4]]
我可以在matplotlib的矢量图中使用。
我尝试使用以下程序执行此操作:
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
当我运行程序时,我得到输出:
soa是[3. 0. -3。 -4。 6. 0。 -2.44 -4。 ]
这不能在matplotlib中绘制为矢量图。我如何调整上面的脚本以使数组成为格式:
[[3,0,-3,-4],[6,0,-2.44,-4]]
其中[3,0,-3,-4]和[6,0,-2.44,-4]是我可以在matplotlib中绘制的向量吗?
答案 0 :(得分:1)
np.append与noraml数组追加不同。
尝试:
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)
答案 1 :(得分:1)
获得所需数组的最简单方法是简单地从数据中堆叠列并在中间放置一个零列
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