您好,登錄后才能下訂單哦!
晚上幫同學用Python腳本繪圖,大概需求是讀取一個txt文件的兩列分別作為x和y的值,繪圖即可,代碼如下:
#coding:utf-8 import numpy as np import matplotlib.pyplot as plt import pylab ## 繪制該文件中的數據 ## 需要引入pylab庫,里面用到的函數和MATLAB里的非常類似 def plotData(x, y): length = len(y) pylab.figure(1) pylab.plot(x, y, 'rx') pylab.xlabel('x') pylab.ylabel('y') pylab.show()#讓繪制的圖像在屏幕上顯示出來 x = [] y = [] x = [float(l.split()[3]) for l in open("07.txt")] y = [float(l.split()[11]) for l in open("07.txt")] plotData(x,y)
如果對跑的效果不滿意可以用matplotlib重繪,代碼如下:
#coding:utf-8 import numpy as np import matplotlib.pyplot as plt import pylab ''' ## 繪制該文件中的數據 ## 需要引入pylab庫,里面用到的函數和MATLAB里的非常類似 def plotData(x, y): length = len(y) pylab.figure(1) pylab.plot(x, y, 'rx') pylab.xlabel('x') pylab.ylabel('y') #讓繪制的圖像在屏幕上顯示出來 ''' x1 = [float(l.split()[3]) for l in open("New.txt")] y1 = [float(l.split()[11]) for l in open("New.txt")] x2 = [float(l.split()[3]) for l in open("Initial.txt")] y2 = [float(l.split()[11]) for l in open("Initial.txt")] plt.plot(x1, y1, 'b',linewidth=2.5, line, label="cosine") plt.plot(x2, y2, 'r',linewidth=2.5, line, label="sine") plt.legend(loc='upper left') plt.show() #plotData(x1,y1) #plotData(x2,y2) #pylab.show()
txt文件前幾列示例如下:
9.999795e-01 5.025123e-04 -6.380358e-03 -4.596714e-03 -5.005160e-04 9.999998e-01 3.144878e-04 -2.001524e-03 6.380515e-03 -3.112871e-04 9.999796e-01 9.154274e-02 9.999096e-01 1.061516e-03 -1.340599e-02 -1.001116e-02 -1.058762e-03 9.999994e-01 2.126022e-04 -4.359704e-03 1.340621e-02 -1.983884e-04 9.999101e-01 1.857373e-01 9.997800e-01 1.386756e-03 -2.092925e-02 -1.753371e-02 -1.395506e-03 9.999989e-01 -4.034725e-04 -6.859665e-03 2.092867e-02 4.325913e-04 9.997809e-01 2.834672e-01 9.995454e-01 2.025309e-03 -3.008212e-02 -2.863192e-02 -2.080074e-03 9.999962e-01 -1.789331e-03 -6.726660e-03 3.007839e-02 1.851091e-03 9.995458e-01 3.873100e-01 9.991403e-01 2.230707e-03 -4.139680e-02 -4.273697e-02 -2.379114e-03 9.999909e-01 -3.536042e-03 -7.303015e-03 4.138854e-02 3.631490e-03 9.991365e-01 5.011396e-01 9.984892e-01 2.145631e-03 -5.490591e-02 -6.000009e-02 -2.394373e-03 9.999871e-01 -4.464951e-03 -8.065471e-03 5.489562e-02 4.589671e-03 9.984816e-01 6.279359e-01 9.974753e-01 2.223074e-03 -7.097932e-02 -8.275693e-02 -2.545053e-03 9.999869e-01 -4.446103e-03 -8.995000e-03 7.096851e-02 4.615524e-03 9.974679e-01 7.669396e-01 9.959798e-01 9.912399e-04 -8.957233e-02 -1.090497e-01 -1.397593e-03 9.999890e-01 -4.473982e-03 -1.033982e-02 8.956691e-02 4.581182e-03 9.959703e-01 9.191805e-01 9.939836e-01 -3.564687e-04 -1.095286e-01 -1.415335e-01 -1.406643e-04 9.999897e-01 -4.531084e-03 -1.195088e-02 1.095291e-01 4.519231e-03 9.939733e-01 1.081913e+00 9.912980e-01 -4.511767e-04 -1.316366e-01 -1.816057e-01 -1.745510e-04 9.999887e-01 -4.741873e-03 -1.604515e-02 1.316372e-01 4.723586e-03 9.912867e-01 1.251378e+00 9.877899e-01 2.927872e-03 -1.557641e-01 -2.325643e-01 -3.900867e-03 9.999747e-01 -5.941284e-03 -2.265364e-02 1.557428e-01 6.476356e-03 9.877764e-01 1.425643e+00 9.834265e-01 7.281907e-03 -1.811611e-01 -2.934590e-01 -8.809762e-03 9.999321e-01 -7.630442e-03 -3.116429e-02 1.810932e-01 9.099964e-03 9.834238e-01 1.604140e+00 9.780772e-01 1.080196e-02 -2.079623e-01 -3.616512e-01 -1.279341e-02 9.998842e-01 -8.233354e-03 -3.933357e-02 2.078493e-01 1.071340e-02 9.781022e-01 1.788640e+00 9.713483e-01 1.158386e-02 -2.373782e-01 -4.366243e-01 -1.400444e-02 9.998657e-01 -8.513365e-03 -4.313507e-02 2.372477e-01 1.159379e-02 9.713800e-01 1.979231e+00
以上這篇Python讀取txt某幾列繪圖的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。