Python實現讀取txt文件并畫三維圖簡單代碼示例
更新時間:2017年12月09日 13:42:24 作者:Mirror_Yu_Chen
這篇文章主要介紹了Python實現讀取txt文件并畫三維圖簡單代碼示例,具有一定借鑒價值,需要的朋友可以參考下。
記憶力差的孩子得勤做筆記!
剛接觸python,最近又需要畫一個三維圖,然后就找了一大堆資料,看的人頭昏腦脹的,今天終于解決了!好了,廢話不多說,直接上代碼!
#由三個一維坐標畫三維散點
#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型轉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()
最關鍵的步驟就是那個string類型轉int類型,之前缺了這一步,死活的報錯,好了,終于搞定!
#畫三維線
# coding: utf - 8 from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt 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型轉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 = fig.gca(projection = '3d') ax.plot(x, y, z) ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') plt.show()
總結
以上就是本文關于Python實現讀取txt文件并畫三維圖簡單代碼示例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關專題。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
tensorflow指定GPU與動態(tài)分配GPU memory設置
今天小編就為大家分享一篇tensorflow指定GPU與動態(tài)分配GPU memory設置,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02PyCharm2019.3永久激活破解詳細圖文教程,親測可用(不定期更新)
這篇文章主要介紹了PyCharm2019.3最新激活碼(注冊碼)破解永久版詳細圖文教程的相關資料,親測可用,需要的朋友可以參考下2020-10-10Python re.findall中正則表達式(.*?)和參數re.S使用
本文主要介紹了Python re.findall中正則表達式(.*?)和參數re.S使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08