【问题标题】:Plotting Repeating Data Set from File using matplotlib and lists使用 matplotlib 和列表从文件中绘制重复数据集
【发布时间】:2014-05-22 03:29:30
【问题描述】:

这是我在这里的第一篇文章,所以我希望一切顺利。

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

【问题讨论】:

  • 你的代码有什么问题?我只能看到一个错误 - MeV_max 应该是 50 而不是 5000
  • 我实际上找到了解决问题的方法,最终使用了一个字典,其中每个能级都是一个键,计数是值

标签: python numpy matplotlib scipy physics


【解决方案1】:

使用字典,其中每个能级是一个键,计数是值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-12
    • 2017-01-03
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多