Python读取txt某几列绘图的方法

作者:nudt_qxx 时间:2021-07-27 06:29:52 

晚上帮同学用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, 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()

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

来源:https://blog.csdn.net/xiangxianghehe/article/details/79156217

标签:Python,txt,绘图
0
投稿

猜你喜欢

  • python中defaultdict用法实例详解

    2022-08-09 17:01:10
  • python调用matlab的m自定义函数方法

    2023-11-11 21:40:52
  • 简介Python中用于处理字符串的center()方法

    2021-04-15 20:47:17
  • python装饰器代码深入讲解

    2023-08-17 20:59:19
  • Python使用Pygame绘制时钟

    2022-08-04 14:10:07
  • PyQT实现菜单中的复制,全选和清空的功能的方法

    2023-08-13 03:09:23
  • js游戏 俄罗斯方块 源代码

    2008-01-24 13:14:00
  • python Matplotlib数据可视化(1):简单入门

    2022-05-11 19:43:16
  • Flask框架通过Flask_login实现用户登录功能示例

    2021-07-19 07:15:38
  • python实现共轭梯度法

    2021-10-06 22:39:01
  • python tornado微信开发入门代码

    2023-11-01 01:04:59
  • 十分钟轻松掌握dataframe数据选择

    2021-03-03 11:11:40
  • python实现矩阵乘法

    2023-11-03 07:41:10
  • PHP get_html_translation_table()函数用法讲解

    2023-06-05 18:36:00
  • asp无限级分类加js收缩伸展功能代码

    2009-12-08 12:25:00
  • 我是如何从玩Photoshop变成老板的

    2008-04-10 11:33:00
  • Python如何读取、写入CSV数据

    2022-02-17 14:03:31
  • 通用的下拉菜单__用DL\\DD\\DT解决无法遮住select的问题

    2008-07-28 13:28:00
  • python flask之模板继承方式

    2022-05-26 03:38:24
  • 自定义Django_rest_framework_jwt登陆错误返回的解决

    2021-02-27 22:07:32
  • asp之家 网络编程 m.aspxhome.com