2

I am quite new to Python. I am trying to plot the 0th and 16th column from a file having a total of 17 columns using matplotlib. I used the following code:

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()

And got the following error:

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

Can you please help?

My data sample:

#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
7
  • FYI index of 16th column is 15 Commented Apr 25, 2017 at 8:58
  • The total number of column is 17. However, I tried plotting the 16th column using (0,15) also. It yielded the same error. Commented Apr 25, 2017 at 9:09
  • As you are trying to plot second to last column can you try (0, -2)? Commented Apr 25, 2017 at 9:21
  • I am trying to plot the last column i.e. the 17th column. Commented Apr 25, 2017 at 9:23
  • So try (0, -1). Commented Apr 25, 2017 at 9:23

1 Answer 1

2

You can use numpy.loadtxt to read the file into a numpy array and then plot the desired columns.

import numpy as np
import matplotlib.pyplot as plt

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

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

From the data you have provided this gives the following graph:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.