【问题标题】:Tuple index out of range in matplotlibmatplotlib 中的元组索引超出范围
【发布时间】:2017-04-25 19:30:16
【问题描述】:

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

【问题讨论】:

  • 第 16 列的 FYI 索引为 15
  • 总列数为 17。但是,我也尝试使用 (0,15) 绘制第 16 列。它产生了同样的错误。
  • 当您尝试绘制倒数第二列时,您可以尝试(0, -2)吗?
  • 我正在尝试绘制最后一列,即第 17 列。
  • 试试(0, -1)

标签: python matplotlib plot


【解决方案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()

根据您提供的数据,这给出了以下图表:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-28
    • 2021-03-02
    • 2018-11-16
    • 2016-04-04
    • 2020-06-08
    • 2017-07-12
    • 1970-01-01
    相关资源
    最近更新 更多