描述:读取data.txt文件,里面每一行都是由空格分隔的x,y,z坐标。本文目标是读取之后将它绘制出来。
要点:
读取文件要用生成器,这样大文件就不用都一次性放进内存。python代码:
#-*-coding:utf-8-*- import numpy as np import re import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def readFile(filepath): with open(filepath,"r") as f: while (True): yield f.readline().strip() filepath = "data.txt" #list = open(filepath,"r").readlines().strip() time = 0 list = [] XYZ = np.zeros((3,400)) for i in readFile(filepath): if (time >=400): break li = [float(j) for j in re.split('\s',i)] XYZ[0][time] = li[0] XYZ[1][time] = li[1] XYZ[2][time] = li[2] time = time + 1 #print (li) print (XYZ) fig = plt.figure() ax = plt.subplot(111,projection='3d') ax.scatter(XYZ[0][:5],XYZ[1][:5],XYZ[2][:5],c='y') #ax.scatter(XYZ[0][100:200],XYZ[1][100:200],XYZ[2][100:200],c='r') #ax.scatter(XYZ[0][200:400],XYZ[1][200:400],XYZ[2][200:400],c='g') plt.show()参考链接:http://blog.csdn.net/eddy_zheng/article/details/48713449
http://jingyan.baidu.com/article/eb9f7b6d6fa886869364e88e.html