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

标签 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

最佳答案

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

相关文章:

python - 如何在 Matplotlib 图中单独标记条形?

python - Matplotlib 不绘制线图;十字形、圆形和三角形都可以

python - fastai - 绘图验证和训练准确性

python - sqlalchemy 的查询返回 AttributeError : 'NoneType' object

python - 当我使用生成器表达式和迭代器从网格上的常规网格插入数据 >2000 次时,我收到 MemError

python - 更改 seaborn 面网格中的线条样式

python - 使用 rcParams 更改 matplotlib 网格颜色

python - 如何从字典的值中删除 '\n'

python - matplotlib 如何指定时间定位器的开始滴答时间戳?

python - 了解matplotlib : plt,图,ax(arr)?