Python 调用Matplotlib 读取txt、csv文件画图

发布时间 2023-04-03 10:57:51作者: cjjlovelife

Python Matplotlib 读取txt、csv文件绘图

show_data.py

import sys
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号

filename = sys.argv[1]
#filename = "2.txt"
file = open(filename)  #打开文档
data = file.readlines() #读取文档数据
para_time = []  #新建列表,用于保存第一列数据
para_pos = []  
para_cs = []  
para_cp = []  
for num in data:
	# split用于将每一行数据用逗号分割成多个对象
    #取分割后的第0列,转换成float格式后添加到para_1列表中
    para_time.append(float(num.split(',')[0]))
    para_pos.append(float(num.split(',')[1]))
    para_cs.append(float(num.split(',')[2]))
    para_cp.append(float(num.split(',')[3]))
plt.figure(1)
plt.xlabel('time')
plt.ylabel('F')
plt.title('time-Cs')
plt.plot(para_time, para_cs)

plt.figure(2)
plt.xlabel('time')
plt.ylabel('F')
plt.title('time-Cp')
plt.plot(para_time, para_cp)

plt.figure(3)
plt.xlabel('位置')
plt.ylabel('F')
plt.title('位置-Cs')
plt.plot(para_pos, para_cs)


plt.figure(4)
plt.xlabel('位置')
plt.ylabel('F')
plt.title('位置-Cp')
plt.plot(para_pos, para_cp)

plt.show()

使用方法

打开终端

python show_data.py "E:\\20230330_163325.txt"