Python 使用matplotlib和列表打印文件中的重复数据集
这是我在这里的第一篇文章,所以我希望它进展顺利 我有一个格式的数据文件(大约2mb) 角度(空间)能量(空间)计数 角度(空间)能量(空间)计数 角度(空间)能量(空间)计数等 (这是从运行约170小时的粒子加速器记录的数据,因此文件很大) 角度从0开始,当能量上升到4500左右时为0,然后 角度增加1,能量再次从0开始上升到4500。这重复了一遍 直到θ=255 我正在尝试创建一个程序,该程序绘制计数的数量与能级的关系,能级是我的x轴,计数是我的y轴。我尝试过许多解决办法,但都没有用 在这方面给予我的任何帮助都将不胜感激 我的代码贴在下面Python 使用matplotlib和列表打印文件中的重复数据集,python,numpy,matplotlib,scipy,physics,Python,Numpy,Matplotlib,Scipy,Physics,这是我在这里的第一篇文章,所以我希望它进展顺利 我有一个格式的数据文件(大约2mb) 角度(空间)能量(空间)计数 角度(空间)能量(空间)计数 角度(空间)能量(空间)计数等 (这是从运行约170小时的粒子加速器记录的数据,因此文件很大) 角度从0开始,当能量上升到4500左右时为0,然后 角度增加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)
使用了一个字典,其中每个能级都是一个键,计数是值代码有什么问题?我只能看到一个错误-
MeV_max
应该是50而不是5000我实际上找到了解决问题的方法,最后使用了一个字典,其中每个能级都是一个键,计数就是值