没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
python 导入数据及作图的实现导入数据及作图的实现
今天小编就为大家分享一篇python 导入数据及作图的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看
看吧
我们经常需要导入数据,按列提取 XY作图
方法一、方法一、
filename='/home/res/user/csluo/test.txt' #将文件名赋值为变量
X,Y,Z=[ ],[ ],[ ] #给三个空列表
with open(filename, 'r') as f: #打开文档
lines=f.readlines() #按行读取数据
for i in lines:
value=[ float(s) for s in line.split( ) ] #将 行数据 以空格分开,并以浮点型赋给value
X=append(value[0]) #将value 的第一个数值 添加到 列表X
Y=append(value[1])
Z=append(value[2])
方法二、方法二、
a = numpy.loadtxt('a.txt')
a[:,0]
方法三、方法三、
>>> import pandas as pd
>>> data = pd.read_csv("aaa.csv")
附录:附录:IRAS16293 的数据的数据 绘图代码绘图代码
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
##################################################################
######################################################################################### ######## 1
##################################################################
#data_a
filename = '/home/luoluo/Documents/python_scripts/Adata_cut/A1_145122-145580.txt'
X10,Y10= [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()] #以空格分开 行 并输出 浮点数
X10.append(value[0])
Y10.append(value[1])
#c2h5oh
filename = '/home/luoluo/Documents/python_scripts/job__XCLASS/job__C2H5OH_A/A1/xclass_spectrum_output.dat'
X11,Y11 = [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X11.append(value[0])
Y11.append(value[1])
#ch3coch3
filename = '/home/luoluo/Documents/python_scripts/job__XCLASS/job__CH3COCH3_A/A1/xclass_spectrum_output.dat'
X12,Y12 = [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X12.append(value[0])
Y12.append(value[1])
#ch3ocho_v18=1
filename = '/home/luoluo/Documents/python_scripts/job__XCLASS/job__CH3OCHO_v18=1_A/A1/xclass_spectrum_output.dat'
X13,Y13 = [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X13.append(value[0])
Y13.append(value[1])
#ch3ocho_v=0
filename = '/home/luoluo/Documents/python_scripts/job__XCLASS/job__CH3OCHO_v=0_A/A1/xclass_spectrum_output.dat'
X14,Y14 = [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X14.append(value[0])
Y14.append(value[1])
################################################### data_b
filename = '/home/luoluo/Documents/python_scripts/Bdata_cut/B1_145122-145580.txt'
X15,Y15= [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X15.append(value[0])
Y15.append(value[1])
#c2h5oh
filename = '/home/luoluo/Documents/python_scripts/job__XCLASS/job__C2H5OH_B/B1/xclass_spectrum_output.dat'
X16,Y16 = [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X16.append(value[0])
Y16.append(value[1])
#ch3coch3
filename = '/home/luoluo/Documents/python_scripts/job__XCLASS/job__CH3COCH3_B/B1/xclass_spectrum_output.dat'
X17,Y17 = [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X17.append(value[0])
Y17.append(value[1])
#ch3ocho_v18=1
filename = '/home/luoluo/Documents/python_scripts/job__XCLASS/job__CH3OCHO_v18=1_B/B1/xclass_spectrum_output.dat'
X18,Y18 = [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X18.append(value[0])
Y18.append(value[1])
#ch3ocho_v=0
filename = '/home/luoluo/Documents/python_scripts/job__XCLASS/job__CH3OCHO_v=0_B/B1/xclass_spectrum_output.dat'
X19,Y19 = [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X19.append(value[0])
Y19.append(value[1])
################################################################### #2 plot
plt.figure(1)
plt.subplot(211)
plt.axis([145122,145580, -1, 15])
new_ticks = np.linspace(145122,145580,10)
plt.xticks(new_ticks)
matplotlib.rcParams['xtick.direction'] = 'in'
matplotlib.rcParams['ytick.direction'] = 'in'
plt.plot(X10,Y10,color='k',lw=1.0,ls='-',label='DATA')
plt.plot(X11,Y11,color='r',lw=1.0,ls='-',label='C2H5OH')
plt.plot(X12,Y12,color='g',lw=1.0,ls='-',label='CH3COCH3')
plt.plot(X13,Y13,color='cyan',lw=1.0,ls='-',label='CH3OCHO v18=1')
plt.plot(X14,Y14,color='b',lw=1.0,ls='-',label='CH3OCHO v=0')
plt.legend(loc='upper left')
#plt.xlabel('Freq(MHZ)')
plt.ylabel('T(K)')
plt.text(145535,13,'IRAS16293A')
#plt.show()
#plt.savefig('8.png')
plt.subplot(212)
plt.axis([145122,145580, -1, 15])
new_ticks = np.linspace(145122,145580,10)
plt.xticks(new_ticks)
plt.plot(X15,Y15,color='k',lw=1.0,ls='-',label='DATA')
plt.plot(X16,Y16,color='r',lw=1.0,ls='-',label='C2H5OH')
plt.plot(X17,Y17,color='g',lw=1.0,ls='-',label='CH3COCH3')
plt.plot(X18,Y18,color='cyan',lw=1.0,ls='-',label='CH3OCHO v18=1')
plt.plot(X19,Y19,color='b',lw=1.0,ls='-',label='CH3OCHO v=0')
plt.legend(loc='upper left')
plt.xlabel('Freq(MHZ)')
plt.ylabel('T(K)')
plt.text(145535,13,'IRAS16293B')
plt.plot([145263,145263],[1,15], color ='grey', linewidth=1, linestyle="--")
plt.annotate("C2H5OH", xy = (145263,1), xytext = (145280,9),arrowprops = dict(facecolor = "y", headlength=10, headwidth =3, width =0.1))
plt.show()
##################################################################
######################################################################################### ######## 2
##################################################################
#data_a
filename = '/home/luoluo/Documents/python_scripts/Adata_cut/A2_145580-146050.txt'
X20,Y20= [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X20.append(value[0])
Y20.append(value[1])
#c2h5oh
filename = '/home/luoluo/Documents/python_scripts/job__XCLASS/job__C2H5OH_A/A2/xclass_spectrum_output.dat'
X21,Y21 = [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X21.append(value[0])
Y21.append(value[1])
#ch3coch3
filename = '/home/luoluo/Documents/python_scripts/job__XCLASS/job__CH3COCH3_A/A2/xclass_spectrum_output.dat'
X22,Y22 = [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X22.append(value[0])
Y22.append(value[1])
#ch3ocho_v18=1
filename = '/home/luoluo/Documents/python_scripts/job__XCLASS/job__CH3OCHO_v18=1_A/A2/xclass_spectrum_output.dat'
X23,Y23 = [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X23.append(value[0])
Y23.append(value[1])
#ch3ocho_v=0
filename = '/home/luoluo/Documents/python_scripts/job__XCLASS/job__CH3OCHO_v=0_A/A2/xclass_spectrum_output.dat'
X24,Y24 = [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X24.append(value[0])
Y24.append(value[1])
################################################### data_b
filename = '/home/luoluo/Documents/python_scripts/Bdata_cut/B2.txt'
X25,Y25= [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X25.append(value[0])
Y25.append(value[1])
#c2h5oh
filename = '/home/luoluo/Documents/python_scripts/job__XCLASS/job__C2H5OH_B/B2/xclass_spectrum_output.dat'
X26,Y26 = [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X26.append(value[0])
Y26.append(value[1])
#ch3coch3
filename = '/home/luoluo/Documents/python_scripts/job__XCLASS/job__CH3COCH3_B/B2/xclass_spectrum_output.dat'
X27,Y27 = [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X27.append(value[0])
Y27.append(value[1])
#ch3ocho_v18=1
filename = '/home/luoluo/Documents/python_scripts/job__XCLASS/job__CH3OCHO_v18=1_B/B2/xclass_spectrum_output.dat'
X28,Y28 = [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X28.append(value[0])
Y28.append(value[1])
#ch3ocho_v=0
filename = '/home/luoluo/Documents/python_scripts/job__XCLASS/job__CH3OCHO_v=0_B/B2/xclass_spectrum_output.dat'
X29,Y29 = [],[]
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
value = [float(s) for s in line.split()]
X29.append(value[0])
Y29.append(value[1])
################################################################### #2 plot
plt.figure(2)
plt.subplot(211)
plt.axis([145580,146050, -1, 15])
new_ticks = np.linspace(145580,146050,10)
plt.xticks(new_ticks)
matplotlib.rcParams['xtick.direction'] = 'in'
matplotlib.rcParams['ytick.direction'] = 'in'
plt.plot(X20,Y20,color='k',lw=1.0,ls='-',label='DATA')
plt.plot(X21,Y21,color='r',lw=1.0,ls='-',label='C2H5OH')
plt.plot(X22,Y22,color='g',lw=1.0,ls='-',label='CH3COCH3')
plt.plot(X23,Y23,color='cyan',lw=1.0,ls='-',label='CH3OCHO v18=1')
plt.plot(X24,Y24,color='b',lw=1.0,ls='-',label='CH3OCHO v=0')
plt.legend(loc='upper left')
#plt.xlabel('Freq(MHZ)')
plt.ylabel('T(K)')
plt.text(146000,13,'IRAS16293A')
plt.plot([145618.5,145618.5],[2.5,15], color ='grey', linewidth=1, linestyle="--")
plt.annotate("CH3OCHO v18=1", xy = (145618,7), xytext = (145650,11),arrowprops = dict(facecolor = "y", headlength=10, headwidth =3, width =0.1))
plt.plot([146020,146020],[7,15], color ='grey', linewidth=1, linestyle="--")
plt.annotate("CH3OCHO v18=1", xy = (146020,6), xytext = (146005,10),arrowprops = dict(facecolor = "y", headlength=10, headwidth =3, width =0.1))
#plt.show()
#plt.savefig('8.png')
plt.subplot(212)
plt.axis([145580,146050, -1, 15])
new_ticks = np.linspace(145580,146050,10)
plt.xticks(new_ticks)
plt.plot(X25,Y25,color='k',lw=1.0,ls='-',label='DATA')
plt.plot(X26,Y26,color='r',lw=1.0,ls='-',label='C2H5OH')
plt.plot(X27,Y27,color='g',lw=1.0,ls='-',label='CH3COCH3')
plt.plot(X28,Y28,color='cyan',lw=1.0,ls='-',label='CH3OCHO v18=1')
plt.plot(X29,Y29,color='b',lw=1.0,ls='-',label='CH3OCHO v=0')
plt.legend(loc='upper left')
plt.xlabel('Freq(MHZ)')
plt.ylabel('T(K)')
plt.text(146000,13,'IRAS16293B')
plt.plot([145618.5,145618.5],[2.5,15], color ='grey', linewidth=1, linestyle="--")
plt.annotate("CH3OCHO v18=1", xy = (145618,7), xytext = (145650,11),arrowprops = dict(facecolor = "y", headlength=10, headwidth =3, width =0.1))
plt.plot([146020,146020],[7,15], color ='grey', linewidth=1, linestyle="--")
plt.annotate("CH3OCHO v18=1", xy = (146020,6), xytext = (145990,10),arrowprops = dict(facecolor = "y", headlength=10, headwidth =3, width =0.1))
剩余17页未读,继续阅读
资源评论
- 金山文档2023-07-26:作者用通俗易懂的语言讲解了复杂的概念,让读者能够轻松理解并实践。
- 朱王勇2023-07-26:作者在文中提供了简洁而实用的代码示例,方便读者快速上手使用。
- 刘璐璐璐璐璐2023-07-26:这篇文章不仅详细介绍了如何导入数据,还讲解了如何进行数据处理和绘图,确实是一篇实用的教程。
- 阿汝娜老师2023-07-26:这个文件清晰地介绍了Python导入数据及作图的实现方法,对初学者非常友好。
- 我就是月下2023-07-26:这个文件不仅帮助读者了解了Python数据处理和作图的基本方法,还给出了一些实际应用的案例,给人以启发。
weixin_38682279
- 粉丝: 9
- 资源: 889
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功