gpt4 book ai didi

python - matplotlib 中的元组索引超出范围

转载 作者:太空宇宙 更新时间:2023-11-04 02:51:54 26 4
gpt4 key购买 nike

我是 Python 的新手。我正在尝试使用 matplotlib 从一个共有 17 列的文件中绘制第 0 列和第 16 列。我使用了以下代码:

import matplotlib.pyplot as plt

plt.plotfile('full_energy.dat', skiprows=3, delimiter=' ', cols=(0,16), names=('Time(ns)', 'Binding Energy(kJ/mol)'))
plt.show()

出现如下错误:

Traceback (most recent call last):
File "./plotting.py", line 6, in <module>
plt.plotfile('full_energy.dat', skiprows=3, delimiter=' ', cols=(16,0), names=('Time(ns)', 'Binding Energy(kJ/mol)'))
File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 2434, in plotfile
xname, x = getname_val(cols[0])
File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 2429, in getname_val
name = r.dtype.names[int(identifier)]
IndexError: tuple index out of range

你能帮忙吗?

我的数据样本:

#Time E_VdW_mm(Protein) E_Elec_mm(Protein)  E_Pol(Protein)  E_Apol(Protein) E_VdW_mm(Ligand)    E_Elec_mm(Ligand)   E_Pol(Ligand)   E_Apol(Ligand)  E_VdW_mm(Complex)   E_Elec_mm(Complex)  E_Pol(Complex)  E_Apol(Complex) Delta_E_mm  Delta_E_Pol Delta_E_Apol    Delta_E_binding

#Complex 1
0.000 0.000 0.000 -4722.345 181.259 0.000 0.000 -8269.165 175.887 -609.445 -9405.764 -11830.800 286.789 -10015.209 1160.710 -70.357 -8924.856
10.000 0.000 0.000 -5360.565 196.823 0.000 0.000 -8328.609 177.112 -593.267 -9477.284 -12452.232 313.072 -10070.551 1236.942 -60.863 -8894.472
20.000 0.000 0.000 -5402.739 191.932 0.000 0.000 -8268.155 177.042 -586.510 -9347.230 -12312.167 306.993 -9933.740 1358.727 -61.981 -8636.994
30.000 0.000 0.000 -5447.859 187.728 0.000 0.000 -8215.354 178.761 -563.052 -9199.273 -12406.912 314.496 -9762.325 1256.301 -51.993 -8558.017
40.000 0.000 0.000 -5415.712 190.240 0.000 0.000 -8211.359 174.982 -536.441 -9427.561 -12199.764 307.642 -9964.002 1427.307 -57.580 -8594.275
50.000 0.000 0.000 -5506.392 190.310 0.000 0.000 -8184.020 174.005 -566.852 -9358.920 -12376.758 308.974 -9925.772 1313.654 -55.341 -8667.459
60.000 0.000 0.000 -5855.564 193.121 0.000 0.000 -8186.953 178.893 -487.185 -9248.351 -12664.296 318.995 -9735.536 1378.221 -53.019 -8410.334

最佳答案

您可以使用 numpy.loadtxt将文件读入一个 numpy 数组,然后绘制所需的列。

import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt("full_energy.dat")

plt.plot(data[:,0], data[:,15])
plt.show()

根据您提供的数据,得出以下图表:

enter image description here

关于python - matplotlib 中的元组索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43605800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com