0

I am wanting to import a .csv file with 3 columns where column 1 is my x values, column 2 is one series, and column 3 is another. I want to plot both series on one plot but when I try to read the csv file and plot it returns a ValueError (see below).

import matplotlib.pyplot as plt
import numpy as np

dir = ""
file = "fig1data.csv"
fn = np.genfromtxt(file, delimiter=',')
x=fn[:,0]
y1=fn[:,1]
y2=fn[:,2]
plt.plot(x, y1, 'b', label=r"1913-1942 anomaly")
plt.plot(x, y2, 'r', label=r"$blah$")
plt.show()

returns the error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-56-0d8440895d40> in <module>()
      4 dir = ""
      5 file = "fig1data.csv"
----> 6 fn = np.genfromtxt(file, delimiter=',')
      7 x=fn[:,0]
      8 y1=fn[:,1]

/usr/lib64/python3.4/site-packages/numpy/lib/npyio.py in genfromtxt(fname, dtype, comments, delimiter, skip_header, skip_footer, converters, missing_values, filling_values, usecols, names, excludelist, deletechars, replace_space, autostrip, case_sensitive, defaultfmt, unpack, usemask, loose, invalid_raise, max_rows)
   1767             # Raise an exception ?
   1768             if invalid_raise:
-> 1769                 raise ValueError(errmsg)
   1770             # Issue a warning ?
   1771             else:

ValueError: Some errors were detected !
    Line #2 (got 3 columns instead of 2)
    Line #3 (got 3 columns instead of 2)
    Line #4 (got 3 columns instead of 2)
    Line #5 (got 3 columns instead of 2)
    Line #6 (got 3 columns instead of 2)
mathwiz97
  • 67
  • 2
  • 11

1 Answers1

0

Is your .csv file formatted correctly?

I was able to get your code to work with a .csv file that looked like this:

col1,col2,col3
0,0.0236852314846,10
1,0.0240463281472,20
2,0.0176584073052,30
3,0.0147662342087,40
4,0.0345340419842,50

However, when I removed the "col3" name, I got the error that you saw. So there may be a discrepancy between the number of column names and the number of columns of data you have.

DavidG
  • 24,279
  • 14
  • 89
  • 82
  • @DavidG Yes I believe so. I have 3 columns with headers. The only I thing I could see causin a problem would be missing values. See the first few lines of my data: year,anomaly,global 1880,, 1881,3.178055556, 1882,0.678055556,-4.6 1883,-0.980277778,-4.7 1884,-2.888611111,-5.2 1885,1.444722222,-5.9 – mathwiz97 Nov 14 '17 at 18:26
  • @vitalie Are the two commas after "1880" in your first row of data a typo? Because there should only be a singe comma separating elements in a row. Also, are your rows separated by newlines or spaces? Could you post your first few lines formatted like I did above to make it clear? – Hunter McGushion Nov 15 '17 at 00:27
  • I actually fixed it, your comment helped in that for some reason not having headers caused an error. I switched to np.loadtxt() and added headers which fixed the issue. For the record, those two commas were not a typo, they are meant to indicate a 3-column field that has two missing data points – mathwiz97 Nov 15 '17 at 04:17