繁体   English   中英

使用matplotlib和列表从文件中绘制重复数据集

[英]Plotting Repeating Data Set from File using matplotlib and lists

这是我在这里的第一篇文章,所以我希望它能顺利进行。

我有一个数据文件(大约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)

使用了一个字典,其中每个能量级别都是关键,而计数是值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM