Model_Plot
1. 在定义函数的时候,用 numpy 而尽量不用 math
math 换成 numpy 后:
2. 重复的程序预先定义好,不要每次写一大段
完整程序:
import numpy as np
import matplotlib.pyplot as plt
def Band(x,k1,a,b,e):
return np.piecewise(x, [x <= (a-b)*e, x > (a-b)*e],\
[lambda x: \
k1*x*(x/100)**a*np.exp(-x/e), \
lambda x: \
k1*x*((a-b)*e/100)**(a-b)*np.exp(b-a)*(x/100)**b])
def CPL(x,k2,g,c):
return k2*x*(x/100)**g*np.exp(-x/c)
def BB(x,k3,t):
return k3*x**3/(np.exp(x/t)-1)
def Plot_Band():
ax.loglog(x,x*Band(x,k1,a,b,e),color='forestgreen',label='Band',alpha=0.7,linewidth=1.8)
def Plot_CPL():
ax.loglog(x,x*CPL(x,k2,g,c),color='darkgoldenrod',label='CPL',alpha=0.7,linewidth=1.8)
def Plot_BB():
ax.loglog(x,x*BB(x,k3,t),color='red',label='BB',alpha=0.7,linewidth=1.8)
def Plot_total_1():
ax.loglog(x,x*Band(x,k1,a,b,e)+x*BB(x,k3,t),color='blue',\
label='Band+BB',alpha=0.8,linewidth=2.5,linestyle='dashed')
def Plot_total_2():
ax.loglog(x,x*CPL(x,k2,g,c)+x*BB(x,k3,t),color='blue',\
label='CPL+BB',alpha=0.8,linewidth=2.5,linestyle='dashed')
def Pic():
ax.set_xlabel('E(KeV)')
ax.set_ylabel(r'$\nu F \nu$')
ax.set_title(name)
ax.grid(True)
ax.legend()
fig.tight_layout()
fig.savefig(str(name)+'.png',dpi=400)
##### plot model
name = 'GRB1'
model = 'Band+BB'
fig,ax = plt.subplots()
x = np.logspace(np.log10(1e-2),np.log10(1e5),200)
ax.set_ylim(1e-5,1e5)
k1,a,b,e = [0.68,-0.23,-2.2,93]
#k2,g,c = []
k3,t = [9.9e-6,57]
Plot_Band(),Plot_BB(),Plot_total_1()
Pic()
name = 'GRB2'
model = 'CPL+BB'
fig,ax = plt.subplots()
x = np.logspace(np.log10(1e-2),np.log10(1e5),200)
ax.set_ylim(1e-5,1e5)
#k1,a,b,e = []
k2,g,c = [0.11,-0.45,3.1e2]
k3,t = [9.5e-5,27]
Plot_CPL(),Plot_BB(),Plot_total_2()
Pic()
name = 'GRB3'
model = 'Band+BB'
fig,ax = plt.subplots()
x = np.logspace(np.log10(1e-2),np.log10(1e5),200)
ax.set_ylim(1e-5,1e5)
k1,a,b,e = [1.7,-0.38,-2.4,1.7e2]
#k2,g,c = []
k3,t = [1.3e-5,1.1e2]
Plot_Band(),Plot_BB(),Plot_total_1()
Pic()
name = 'GRB4'
model = 'CPL+BB'
fig,ax = plt.subplots()
x = np.logspace(np.log10(1e-2),np.log10(1e5),200)
ax.set_ylim(1e-5,1e5)
#k1,a,b,e = []
k2,g,c = [1.7,-0.91,68]
k3,t = [3.4e-6,1.4e2]
Plot_CPL(),Plot_BB(),Plot_total_2()
Pic()
name = 'GRB5'
model = 'CPL+BB'
fig,ax = plt.subplots()
x = np.logspace(np.log10(1e-2),np.log10(1e5),200)
ax.set_ylim(1e-5,1e5)
#k1,a,b,e = []
k2,g,c = [0.51,-0.46,69]
k3,t = [1.2e-6,93]
Plot_CPL(),Plot_BB(),Plot_total_2()
Pic()
name = 'GRB6'
model = 'Band+BB'
fig,ax = plt.subplots()
x = np.logspace(np.log10(1e-2),np.log10(1e5),200)
ax.set_ylim(1e-5,1e5)
k1,a,b,e = [0.11,-1.1,-4.1,5.7e2]
#k2,g,c = []
k3,t = [1.7e2,0.61]
Plot_Band(),Plot_BB(),Plot_total_1()
Pic()
name = 'GRB7'
model = 'CPL+BB'
fig,ax = plt.subplots()
x = np.logspace(np.log10(1e-2),np.log10(1e5),200)
ax.set_ylim(1e-5,1e5)
#k1,a,b,e = []
k2,g,c = [1.7,-0.066,52]
k3,t = [1.8e2,0.52]
Plot_CPL(),Plot_BB(),Plot_total_2()
Pic()
name = 'GRB8'
model = 'CPL+BB'
fig,ax = plt.subplots()
x = np.logspace(np.log10(1e-2),np.log10(1e5),200)
ax.set_ylim(1e-5,1e5)
#k1,a,b,e = []
k2,g,c = [0.21,-0.43,59]
k3,t = [75,0.51]
Plot_CPL(),Plot_BB(),Plot_total_2()
Pic()
结果示例: