1
這是我的第一篇文章,所以我希望它順利。使用matplotlib繪製從文件重複數據集並列出
我在格式 角度(空間)能量(空間)有數據的文件(約2MB)計數 角度(空間)能量(空間)計數 角度(空間)能量(空間)計數等
(這是從粒子加速器記錄的數據運行〜170小時,所以該文件是大)
角在0開始時,和爲0,而能上升到約4500,然後 角增加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)
什麼是你的代碼錯誤?我只能看到一個錯誤 - 「MeV_max」應該是50而不是5000 –
我實際上已經找到了解決問題的方法,最終使用了一個字典,其中每個能級都是關鍵字,並且計數值 – Alexander94