記憶力差的孩子得勤做筆記!
剛接觸python,最近又需要畫一個三維圖,然后就找了一大堆資料,看的人頭昏腦脹的,今天終于解決了!好了,廢話不多說,直接上代碼!
#由三個一維坐標(biāo)畫三維散點
#coding:utf-8 import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d.axes3d import Axes3D x = [] y = [] z = [] f = open("data//record.txt") line = f.readline() while line: c,d,e = line.split() x.append(c) y.append(d) z.append(e) line = f.readline() f.close() #string型轉(zhuǎn)int型 x = [ int( x ) for x in x if x ] y = [ int( y ) for y in y if y ] z = [ int( z ) for z in z if z ] print x fig=plt.figure() ax=Axes3D(fig) ax.scatter3D(x, y, z) ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') plt.show() 最關(guān)鍵的步驟就是那個string類型轉(zhuǎn)int類型,之前缺了這一步,死活的報錯,好了,終于搞定!
#畫三維線
#coding: utf - 8from mpl_toolkits.mplot3dimport axes3dimport matplotlib.pyplot as pltx = []y = []z = []f = open("data//record.txt")line = f.readline()while line: c, d, e = line.split()x.append(c)y.append(d)z.append(e)line = f.readline()f.close()# string型轉(zhuǎn)int型x = [int(x) for x in x if x]y = [int(y) for y in y if y]z = [int(z) for z in z if z]# print xfig = plt.figure()ax = fig.gca(projection = '3d')ax.plot(x, y, z)ax.set_xlabel('x')ax.set_ylabel('y')ax.set_zlabel('z')plt.show()總結(jié)
以上就是本文關(guān)于Python實現(xiàn)讀取txt文件并畫三維圖簡單代碼示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
新聞熱點
疑難解答