这是我在这里的第一篇文章,所以我希望它进展顺利。在
我有一个格式的数据文件(大约2mb) 角度(空间)能量(空间)计数 角度(空间)能量(空间)计数 角度(空间)能量(空间)计数等
(这是粒子加速器运行170小时后记录的数据,因此文件很大)
角度从0开始,当能量增加到4500,然后 角度增加1,能量从0开始,上升到4500。这样重复 直到θ=255。在
我正在尝试创建一个程序来绘制计数与能级的关系,能级是x轴,计数是y轴。我试过很多办法,但都没有用。在
如果能在这方面给我任何帮助,我将不胜感激。在
我的代码张贴在下面。在
import matplotlib.pyplot as plt
import numpy as np
import pylab
from numpy import *
from matplotlib.pyplot import *
import math
import sys
import scipy.optimize
"""
Usage
---------------
Takes a file in the format of
Theta |Rel_MeV |Counts
97 4024 0
97 4025 0
97 4026 6
97 4027 2
and graphs it
fileURL is the input for the file to put into the program
txt_Title is the graph label
"""
DEBUG = 1
fileURL = './ne19_peaks_all.dat'
txt_Title = 'Oxygen and Alpha Particle Relative Energy'
MeV_divide_factor = 100
ptSize = 5
MarkerType = '+'
MeV_max = 5000
def main():
# Read the file.
f2 = open(fileURL, 'r')
# read the whole file into a single variable, which is a list of every row of the file.
lines = f2.readlines()
f2.close()
# initialize some variable to be lists:
list_MeV = []
list_counts = []
for i in range(MeV_max):
list_MeV.append(i)
list_counts.append(0)
# scan the rows of the file stored in lines, and put the values into some variables:
for line in lines:
p = line.split()
MeV = float(p[1])/MeV_divide_factor
count = float(p[2])
list_counts[int(MeV)] += count
x_arr = np.array(list_MeV)
y_arr = np.array(list_counts)
plt.plot(x_arr, y_arr, MarkerType)
plt.title(txt_Title)
plt.show()
return 0
def func(x, a, b):
return a*x + b
if __name__ == '__main__':
status = main()
sys.exit(status)
使用字典,其中每个能级都是一个键,计数是值
相关问题 更多 >
编程相关推荐