Python读取txt指定列并绘图的实现方法
本文介绍如何使用Python读取txt文件中的指定列数据,并利用matplotlib库进行可视化绘图。通过示例代码展示如何提取特定列作为x和y轴数据,绘制散点图或折线图,并添加标签、图例及样式设置,帮助读者快速实现数据可视化需求。
Python读取txt指定列并绘图的方法
在数据处理与分析中,经常需要从文本文件中提取特定列的数据进行可视化。Python提供了强大的库如 numpy 和 matplotlib,可以方便地实现这一目标。本文将介绍如何读取txt文件中的指定列,并使用matplotlib进行绘图。
1. 基本绘图方法
首先,我们需要导入必要的库:numpy、matplotlib.pyplot 和 pylab。pylab库提供了类似MATLAB的绘图接口,适合快速绘图。
#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)上述代码中,我们读取了名为 07.txt 的文件,提取了第4列(索引为3)和第12列(索引为11)的数据,分别作为x和y轴的值。然后调用 plotData 函数进行绘图,使用红色叉号('rx')标记数据点。
2. 使用matplotlib进行更灵活的绘图
虽然pylab库提供了简单的绘图接口,但matplotlib库提供了更多的自定义选项,如线条样式、颜色和图例等。以下代码展示了如何使用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, linestyle="-", label="cosine")
plt.plot(x2, y2, 'r',linewidth=2.5, linestyle="-", label="sine")
plt.legend(loc='upper left')
plt.show()
#plotData(x1,y1)
#plotData(x2,y2)
#pylab.show()在上述代码中,我们读取了两个文件:New.txt 和 Initial.txt,分别提取了第4列和第12列的数据。然后使用matplotlib绘制了两条折线图,分别用蓝色和红色表示,并设置了线条宽度为2.5,线型为实线。最后,通过 plt.legend 添加了图例,并使用 plt.show() 显示图像。
3. 数据示例
以下是一个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通过上述示例,我们可以看到如何从txt文件中提取指定列的数据,并使用Python进行绘图。希望本文能帮助读者更好地理解和应用Python进行数据可视化。
以上就是Python读取txt指定列并绘图方法的详细内容,更多关于Python数据可视化的资料请关注本站其它相关文章!
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













