这是我在这里的第一篇文章,所以我希望它能顺利进行。
我有一个数据文件(大约2mb),格式如下:角度(空间)能量(空间)计数角度(空间)能量(空间)计数角度(空间)能量(空间)计数,等等。
(这是运行约170小时的粒子加速器记录的数据,因此文件很大)
角度从0开始,当能量增加到大约4500的时候,角度是0,然后角度增加1,能量再次从0开始,增加到4500。重复这一过程,直到theta = 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)发布于 2014-05-23 10:06:29
使用字典,其中每个能级是一个键,计数是值
https://stackoverflow.com/questions/23797368
复制相似问题