matplotlib中的元组索引超出范围

时间:2017-04-25 08:47:45

标签: python matplotlib plot

我对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

1 个答案:

答案 0 :(得分:1)

您可以使用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