python - IndexError:索引 45 超出轴 0 的范围,大小为 10
问题描述
我试图从以下 python 脚本文件中获取绘图,但出现错误:
Traceback (most recent call last):
File "./plot_inteqp.py", line 37, in <module>
plt.xticks(x[ind], map(to_mathrm, ['W', 'L', r'\Gamma', 'X', 'W', 'K']))
IndexError: index 45 is out of bounds for axis 0 with size 10
脚本文件是
#!/usr/bin/env python
#coding: utf-8
import numpy as np
import matplotlib as mpl
#mpl.use('Agg')
import matplotlib.pyplot as plt
data = np.loadtxt('bandstructure.dat')
bands = data[:,1]
kpts = data[:,2:5]
emf = data[:,5]
eqp = data[:,6]
emf -= np.amax(emf[bands==4])
eqp -= np.amax(eqp[bands==4])
def get_x(ks):
# X axis is \int_0^k |dk|
dk_vec = np.diff(ks, axis=0)
dk_len = np.linalg.norm(dk_vec, axis=1)
return np.insert(np.cumsum(dk_len), 0, 0.)
xmin, xmax = np.inf, -np.inf
bands_uniq = np.unique(bands).astype(np.int)
for ib in bands_uniq:
cond = bands==ib
x = get_x(kpts[cond])
xmin, xmax = min(xmin, x[0]), max(xmax, x[-1])
lmf, = plt.plot(x, emf[cond], '--', color='b', lw=1.5, zorder=2)
lqp, = plt.plot(x, eqp[cond], '-', color='r', lw=1.5, zorder=1)
def to_mathrm(s):
return r'$\mathrm{%s}$'%(s)
ind = [0, 45, 95, 145, 173, 199]
plt.xticks(x[ind], map(to_mathrm, ['W', 'L', r'\Gamma', 'X', 'W', 'K']))
for idx in ind:
plt.axvline(x[idx], color='k')
plt.title('Si bandstructure: inteqp.real.x code')
plt.axhline(0, color='#cccccc', zorder=-10)
plt.xlim(xmin, xmax)
plt.ylim(-12.5, 7.5)
plt.legend([lmf, lqp], ['LDA', 'GW'], loc='lower right')
plt.ylabel('Energy (eV)')
plt.savefig('bandstructure_inteqp.pdf')
你能告诉我错误是否是由于python版本造成的吗?请帮帮我。谢谢
解决方案
推荐阅读
- python - 如何估计噪声层后面的高斯分布?
- sql - 如何避免在 mysql 中的 SELECT 之前写入模式名称?
- sql-server - F# - FSharp.Data.SqlClient – How to specify timeout for Update
- tinkerpop - janusgraph index performance
- python - Why pandas asfreq produces dates not in the input list?
- java - 如何使用 org.ow2.asm 6.0 修改 Java 字节码
- vuejs2 - 如何在 Element UI 折叠的水平菜单中编辑打开延迟
- mysql - MYSQL数据库计算
- excel - 拆分字符串两次
- ios - Nativescript - 该应用程序无法安装在模拟器上