Python读取txt文件并且画出图像,在曲线label中添加上下标

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import MultipleLocator


def padding_data(x):
    array1 = []
    up_coefficient = [1.122e-08, - 5.613e-06 , 0.0008606, - 0.0378, 0.4916]
    up_coefficient = np.array(up_coefficient)
    up_equ = np.poly1d(up_coefficient)
    #print('up_equ is :\n', up_equ)
    for i in range(0, 13):
        x_mid1 = 90 * i / 12 + 39
        y_mid1 = x * up_equ(x_mid1)
        array1.append(y_mid1)
    array1.append(x)
    down_coefficient = [-4.203e-09, 3.787e-06, - 0.00124, 0.1677, - 6.957]
    down_coefficient = np.array(down_coefficient)
    down_equ = np.poly1d(down_coefficient)
    #print('down_equ is :\n', down_equ)
    for i in range(0, 13):
        x_mid2 = 158 * i / 12 + 129
        y_mid2 = x * down_equ(x_mid2)
        array1.append(y_mid2)
    return array1


if __name__ == '__main__':
    filename = 'Pu.txt'
    amount = []
    solve_data = 0.0
    with open(filename, 'r') as f:  # 1
        lines = f.readlines()  # 2
        for line in lines:  # 3
            value = [float(s) for s in line.split()]  # 4
            if value[0] >= 2:
                x1 = []
                solve_data = value[0]
                x1 = padding_data(solve_data)
                amount.extend(x1)
            else:
                amount.append(value[0])  # 5
    axis = 0
    a_axis = []
    for i in range(0, len(amount)):
        if i % 5 == 0:
            axis += 6
        elif i % 5 == 1:
            axis += 5
        elif i % 5 == 2:
            axis += 7
        elif i % 5 == 3:
            axis += 6
        elif i % 5 == 4:
            axis += 5
        a_axis.append(axis)

    plt.plot(a_axis, amount, label='$\mathregular{{Pu}_{94}^{239}}$')
    font2 = {'family': 'Times New Roman',
             'weight': 'normal',
             'size': 15,
             }
    x_major_locator = MultipleLocator(80)
    # Set the spacing on the X-axis to 10 and store it in a variable
    ax = plt.gca()
    # ax is an example of two axes
    ax.xaxis.set_major_locator(x_major_locator)
    # Set the main scale on the X-axis to a multiple of 10
    plt.ylim(0, 9)
    plt.xlim(0, axis)
    plt.ylabel("Energy(Mev)", font2)
    plt.xlabel("Times(us)", font2)
    plt.grid()
    plt.legend(loc='upper left')
    plt.show()

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值