3

I am trying to plot a graph in python with matplotlib. The file i am trying to give as input is txt file with no delimiter. It has many columns and I am interested in col[2] and col[4]. The data could be str or int or float.

Input file

3401  1772  1  0.0002  3498.0
3840  3730  5  0.001  4658.0
3439  651  13  0.0026  22208.0
5069  3354  2  0.0004  3510.0
5252  4001  5  0.001  3468.0
5417  2970  5  0.001  4224.0
4653  3928  5  0.001  10132.0
1681  1028  2  0.0004  9399.0
2908  2615  4  0.0008  19306.0

Code:

import numpy as np
import matplotlib.pyplot as plt
from pylab import*
import math
from matplotlib.ticker import LogLocator

plt.plotfile('edge_per_one_1.txt', delimiter=' ', cols=(2,4), names=('col2','col4'), marker='o')

plt.show()

Error:

Traceback (most recent call last):
  File "plot_data.py", line 7, in <module>
    plt.plotfile('edge_per_one_1.txt', delimiter=' ', cols=(2,4), names=('col2','col4'), marker='o')
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 1894, in plotfile
    xname, x = getname_val(cols[0])
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 1889, in getname_val
    name = r.dtype.names[int(identifier)]
IndexError: tuple index out of range
1
  • All of them seem to be correct. Thanks to everyone. Commented Sep 4, 2014 at 13:05

3 Answers 3

1

You have missed column names in names argument. Also your input file seems to have DOUBLE SPACES as delimiters. Delimiters should be SINGLE CHAR items(space or comma).

import matplotlib.pyplot as plt
plt.plotfile('edge_per_one_1.txt', delimiter=' ', cols=(2,4), 
              names=('col1','col2','col3','col4','col5'), marker='o')
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

1

Clue is in the stacktrace:

File "pyplot.py", line 2318, in getname_val
name = r.dtype.names[int(identifier)]
IndexError: tuple index out of range

Looks like the getname_val passed is too short. Looking in the code itself:

        elif is_numlike(identifier):
        name = r.dtype.names[int(identifier)]

It looks like it is trying to access the names by index you provided. This means that you must provide all of the column names to plotfile,

plt.plotfile('edge_per_one_1.txt', delimiter=' ', cols=(2,4), names=('col1','col2','col3','col4','col5'), marker='o')

In short: the names argument requires you to give the names of all columns

Comments

0

You could also provide axes label using pyplot :

plt.plotfile('edge_per_one_1.txt', delimiter=' ', cols=(2,4), marker='o')
plt.xlabel("col2")
plt.ylabel("col4")

Comments

Your Answer

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