分离数据集的第一行并重新分配给新矩阵-python

时间:2018-12-09 19:18:48

标签: python arrays matrix

我正在将文本文件读入包含x,y和z数组(描述坐标位置)的python中。我想从x,y,z数组中排除第一行,即源的坐标,并使用这些值创建一个新的数组xs,ys,zs。这样我就可以分别绘制源位置,如代码的散点图所示。

代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.ticker import NullFormatter

x=[]
y=[]
z=[]

data = np.genfromtxt('DATA.txt', delimiter=',',dtype=float, 
usecols=np.arange(0,3))


for row in data:
x.append(row[0])
y.append(row[1])
z.append(row[2])    

print(x)
print(y)
print(z)

'plt.scatter(x,y, s = 1800, alpha=0.3)
plt.scatter(xs,ys, marker="+", s=1500)
plt.title('Data set 0 - xy scatter')
plt.xlabel('x')
plt.ylabel('y')
plt.xlim(-3, 3)
plt.ylim(-3, 3)
plt.show()'

结果:

 [-0.29999999999999999, -1.1000000000000001, -1.7, -0.73999999999999999, 
-0.14000000000000001, -0.23000000000000001, -12.0, -1.8, 
-1.1699999999999999, -17.0, 0.42999999999999999, -0.57999999999999996, 
-1.5800000000000001, 9.8000000000000007, -0.76000000000000001, 
-0.97999999999999998, -1.1000000000000001]
[-1.1000000000000001, -2.4500000000000002, -4.4000000000000004, -1.77, 
-0.34000000000000002, -0.56999999999999995, -28.0, -4.5, 
-2.6299999999999999, -47.0, 0.65000000000000002, -1.5800000000000001, -3.79, 
23.350000000000001, -1.5, -2.4900000000000002, -2.7999999999999998]
[1.3600000000000001, 2.7000000000000002, 5.5, 2.3999999999999999, 
0.23999999999999999, 0.54000000000000004, 32.0, 6.9000000000000004, 3.25, 
58.0, -1.0, 1.9399999999999999, 4.5999999999999996, -28.289999999999999, 
1.3500000000000001, 3.7000000000000002, 3.2599999999999998]

所需结果:

[-1.1000000000000001, -1.7, -0.73999999999999999, 
-0.14000000000000001, -0.23000000000000001, -12.0, -1.8, 
-1.1699999999999999, -17.0, 0.42999999999999999, -0.57999999999999996, 
-1.5800000000000001, 9.8000000000000007, -0.76000000000000001, 
-0.97999999999999998, -1.1000000000000001]
[-2.4500000000000002, -4.4000000000000004, -1.77, 
-0.34000000000000002, -0.56999999999999995, -28.0, -4.5, 
-2.6299999999999999, -47.0, 0.65000000000000002, -1.5800000000000001, -3.79, 
23.350000000000001, -1.5, -2.4900000000000002, -2.7999999999999998]
[2.7000000000000002, 5.5, 2.3999999999999999, 
0.23999999999999999, 0.54000000000000004, 32.0, 6.9000000000000004, 3.25, 
58.0, -1.0, 1.9399999999999999, 4.5999999999999996, -28.289999999999999, 
1.3500000000000001, 3.7000000000000002, 3.2599999999999998]

[-0.29999999999999999]
[-1.1000000000000001]
[1.3600000000000001]

在此先感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

在下面添加此代码:

xs = x[0]
ys = y[0]
zs = z[0]

x = x[1:-1] #= [-1.1, -1.7, -0.74, -0.14, -0.23, -12.0, -1.8, -1.17, -17.0, 0.43, -0.58,-1.58, 9.8, -0.76, -0.98]

y = y[1:-1] #= [-2.4500000000000002, -4.4000000000000004, -1.77, -0.34000000000000002, -0.56999999999999995, -28.0, -4.5, -2.6299999999999999, -47.0, 0.65000000000000002, -1.5800000000000001, -3.79, 23.350000000000001, -1.5, -2.4900000000000002, -2.7999999999999998]

z = z[1:-1] #= [2.7000000000000002, 5.5, 2.3999999999999999, 0.23999999999999999, 0.54000000000000004, 32.0, 6.9000000000000004, 3.25, 58.0, -1.0, 1.9399999999999999, 4.5999999999999996, -28.289999999999999, 1.3500000000000001, 3.7000000000000002, 3.2599999999999998]

print('\n', x, '\n', y, '\n', z, '\n', [xs], '\n', [ys], '\n', [zs])

这将输出

[-1.1000000000000001, -1.7, -0.73999999999999999, 
-0.14000000000000001, -0.23000000000000001, -12.0, -1.8, 
-1.1699999999999999, -17.0, 0.42999999999999999, -0.57999999999999996, 
-1.5800000000000001, 9.8000000000000007, -0.76000000000000001, 
-0.97999999999999998, -1.1000000000000001]

[-2.4500000000000002, -4.4000000000000004, -1.77, 
-0.34000000000000002, -0.56999999999999995, -28.0, -4.5, 
-2.6299999999999999, -47.0, 0.65000000000000002, -1.5800000000000001, -3.79, 
23.350000000000001, -1.5, -2.4900000000000002, -2.7999999999999998]

[2.7000000000000002, 5.5, 2.3999999999999999, 
0.23999999999999999, 0.54000000000000004, 32.0, 6.9000000000000004, 3.25, 
58.0, -1.0, 1.9399999999999999, 4.5999999999999996, -28.289999999999999, 
1.3500000000000001, 3.7000000000000002, 3.2599999999999998]

[-0.29999999999999999]

[-1.1000000000000001]

[1.3600000000000001]