之前用 matplotlib.pyplot.quiver 画能级(横线),发现画出来的一些图打印出来以后,粗横线中有细横线。所以网上搜了一下,试了一下新的办法:matplotlib.pyplot.hlines,发现不错,记在这里。
在下面的代码块中,Ln16 使用 plt.hlines 画能级图,如果需要仔细调线型,可以看 Ln13 中的 LineCollection 里的东西。
画出来的效果图在最后面。
#coding=utf-8
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
#help(matplotlib.collections.LineCollection); exit(1)
def EnergyLevels( plt, xleft, xlabel, energy, flaglabel ):
plt.hlines(energy, xleft, xleft + 0.7, colors='black')
for i in range(len(energy)):
t = energy[i]
#plt.quiver(xleft, t, 1, 0, width = 0.003, scale=2, headlength=0, headwidth=0, scale_units='x')
if flaglabel : plt.text(xlabel, t, '%d$^+$' % (2 * i), fontsize=10)
plt.figure(figsize=(10,4.8))
plt.cla()
ax = plt.axes()
plt.xticks([])
plt.axis(xmin = -11, xmax = 13, ymin = -2, ymax = 8)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.tick_params(right=True,direction='in',length=5,width=1)
plt.ylabel("$E_x$ (MeV)", fontsize = 15)
# Xe124 data
J = [ 0, 2, 4, 6, 8, 10, 12, 14, 16 ]
PVPC_Ex = [ 0, 0.152, 0.492, 1.000, 1.644, 2.475, 3.265, 4.237, 5.686 ]# Egs = -29.538
PHF_Ex = [ 0, 0.138, 0.445, 0.899, 1.483, 2.187, 3.002, 3.942, 5.001 ]# Egs = -28.956
EXP_Ex = [ 0, 0.354, 0.879, 1.548, 2.331, 3.171, 3.883, 4.613, 5.466 ]
xleft = -9; xlabel = xleft-1; EnergyLevels(plt, xleft, xlabel, EXP_Ex, True )
plt.text(xleft-0.5, -1, "Expt.", fontsize=10 ) # EXP
xleft = -8; xlabel = xleft-1; EnergyLevels(plt, xleft, xlabel, PVPC_Ex, False )
plt.text(xleft, -1, "PVPC", fontsize=10 ) # PVPC
xleft = -7; xlabel = xleft-1; EnergyLevels(plt, xleft, xlabel, PHF_Ex, False )
plt.text(xleft+0.5, -1, "PHF", fontsize=10 ) # PHF
plt.text(-8, -2, "$^{124}Xe$", fontsize = 15)
# Xe126 data
J = [ 0, 2, 4, 6, 8, 10, 12, 14, 16 ]
PVPC = [ -29.055, -28.8698, -28.5653, -28.1574, -27.9447, -27.092, -26.339, -25.5057, -25.1536 ]
PVPC_Ex = np.array(PVPC) - PVPC[0]
PHF = [ -28.706, -28.544, -28.266, -27.882, -27.393, -26.802, -26.106, -25.306, -24.382 ]
PHF_Ex = np.array(PHF) - PHF[0]
EXP_Ex = [ 0, 0.354, 0.879, 1.548, 2.331, 3.171, 3.883, 4.613, 5.466 ]
xleft = -3; xlabel = xleft-1; EnergyLevels(plt, xleft, xlabel, EXP_Ex, True )
plt.text(xleft-0.5, -1, "Expt.", fontsize=10 ) # EXP
xleft = -2; xlabel = xleft-1; EnergyLevels(plt, xleft, xlabel, PVPC_Ex, False )
plt.text(xleft, -1, "PVPC", fontsize=10 ) # PVPC
xleft = -1; xlabel = xleft-1; EnergyLevels(plt, xleft, xlabel, PHF_Ex, False )
plt.text(xleft+0.5, -1, "PHF", fontsize=10 ) # PHF
plt.text(-2, -2, "$^{126}Xe$", fontsize = 15)
# Ba126 data
J = [ 0, 2, 4, 6, 8, 10, 12, 14, 16 ]
PVPC_Ex = [ 0, 0.175, 0.579, 1.208, 2.059, 3.118, 4.386, 5.574, 7.484 ]
# lineartol = 1E-4, Egs = -39.4886
PHF_Ex = [ 0, 0.173, 0.574, 1.200, 2.046, 3.105, 4.369, 5.828, 7.473 ]
# Egs = -39.41
EXP_Ex = [ 0, 0.256, 0.711, 1.332, 2.090, 2.942, 3.747, 4.420, 5.245 ]
xleft = 3; xlabel = xleft-1; EnergyLevels(plt, xleft, xlabel, EXP_Ex, True )
plt.text(xleft-0.5, -1, "Expt.", fontsize=10 ) # EXP
xleft = 4; xlabel = 3.7; EnergyLevels(plt, xleft, xlabel, PVPC_Ex, False )
plt.text(xleft, -1, "PVPC", fontsize=10 ) # PVPC
xleft = 5; xlabel = 3.7; EnergyLevels(plt, xleft, xlabel, PHF_Ex, False )
plt.text(xleft+0.5, -1, "PHF", fontsize=10 ) # PHF
plt.text(4, -2, "$^{126}Ba$", fontsize = 15)
# Ba128 data
J = [ 0, 2, 4, 6, 8, 10, 12, 14, 16 ]
PVPC = [ -39.203, -39.0268, -38.6209, -37.99, -37.1414, -36.0841, -34.8343, -30.5374, -31.6854 ] # lineartol = 1E-4
PVPC_Ex = np.array(PVPC) - PVPC[0]
PHF = [ -39.119, -38.946, -38.542, -37.915, -37.071, -36.020, -34.774, -33.343, -31.739]
PHF_Ex = np.array(PHF) - PHF[0]
EXP_Ex = [ 0, 0.284, 0.763, 1.407, 2.189, 3.082, 3.988, 4.646, 5.496 ]
xleft = 9; xlabel = xleft-1; EnergyLevels(plt, xleft, xlabel, EXP_Ex, True )
plt.text(xleft-0.5, -1, "Expt.", fontsize=10 ) # EXP
xleft = 10; xlabel = 5.7; EnergyLevels(plt, xleft, xlabel, PVPC_Ex, False )
plt.text(xleft, -1, "PVPC", fontsize=10 ) # PVPC
xleft = 11; xlabel = 5.7; EnergyLevels(plt, xleft, xlabel, PVPC_Ex, False )
plt.text(xleft+0.5, -1, "PHF", fontsize=10 ) # PHF
plt.text(10, -2, "$^{128}Ba$", fontsize = 15)
plt.savefig("tex/XeBa.spectrum.eps")
plt.savefig("tex/XeBa.spectrum.png")
plt.show()