我的数据格式如下:
87.2224 -0.0453
87.1561 -0.1580
>
90.8429 -0.1164
90.3849 -0.1020
90.2667 -0.1246
>
87.1002 -0.0553
87.1561 -0.1580
>其中,列是一条线的x,y坐标,每个>分隔一条新线。
如何使用numpy (或其他Python库)正确地读取它,并使用matplotlib绘制它,以便可以绘制三行?
或者,是否可以在用Python读取数据之前对数据进行格式化,以使这一过程更容易?
谢谢!:)
发布于 2019-05-15 01:26:27
下面应该可以做到这一点:
x=[]
y=[]
with open("data.txt") as file:
for line in file:
if ">" not in line:
coords = line.split()
# print(coords)
x.append(float(coords[0]))
y.append(float(coords[1]))发布于 2019-05-15 01:31:41
通过将>指定为注释,可以简单地使用np.loadtxt读入数据。这将忽略这些行。您可以稍后将这些数组整形为两个位/块,以绘制单独的线条。
import numpy as np
x, y = np.loadtxt('filename.txt', unpack=True, comments='>')
# (array([87.2224, 87.1561, 90.3849, 90.2667, 87.1002, 87.1561]),
# array([-0.0453, -0.158 , -0.102 , -0.1246, -0.0553, -0.158 ]))发布于 2019-05-15 02:00:15
@pdrersin给了我足够的答案:
x=[]
y=[]
with open("data.txt") as file:
for line in file:
if ">" not in line:
coords = line.split()
x.append(float(coords[0]))
y.append(float(coords[1]))
if ">" in line:
plt.plot(x,y)
x=[]
y=[]这将单独打印每条线,而不连接其末端。
https://stackoverflow.com/questions/56135290
复制相似问题