0

I am trying to plot from a csv file with column 1 as a datetime value, as below

 27-08-2016 08:43   21.38329164

using this code:

from matplotlib import pyplot as plt
from matplotlib import style
import numpy as np
import datetime as dt
from datetime import datetime
import matplotlib.dates as mdates

style.use('ggplot')

x,y = np.genfromtxt('I112-1a.csv', unpack=True,dtype=None, delimiter = ',', converters={0: lambda x: datetime.strptime(x, '%d-%m-%Y %H:%M')})


plt.title('Panel Charge')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()

I am getting this error:

    x,y = np.genfromtxt('I112-1a.csv', unpack=True,dtype=None, delimiter = ',', converters={0: lambda x: datetime.strptime(x, '%d-%m-%Y %H:%M')})
ValueError: too many values to unpack

Please help! Thanks

warrenfitzhenry
  • 2,209
  • 8
  • 34
  • 56
  • you do not need and cannot assign the result of the `np.genfromtxt()` to two variables since it is a single object, ndarray, in this case. You will either have to unpack it using `x, y = *np.genfromtxt(....)` or just assign it to x and plot that. `matplotlib` might handle the unpacking itself – Ma0 Sep 22 '16 at 09:24
  • Please avoid answering in comment section. – Sayali Sonawane Sep 22 '16 at 10:55

2 Answers2

0

Source: numpy.genfromtxt

numpy.genfromtxt  Returns:ndarray
Data read from the text file. If usemask is True, this is a masked array.

@Ev. Kounis pointed out in comment section: numpy.genfromtxt returns only one array and you are assigning it to two parameters x and y.

Please update your code as follows:

data = np.genfromtxt('I112-1a.csv', unpack=True,dtype=None, delimiter = ',', converters={0: lambda x: datetime.strptime(x, '%d-%m-%Y %H:%M')})

For example,

If your data file is structured like this

col1, col2, col3
   1,    2,    3
  10,   20,   30
 100,  200,  300

then numpy.genfromtxt can interpret the first line as column headers using the names=True option. With this you can access the data very conveniently by providing the column header:

data = np.genfromtxt('data.txt', delimiter=',', names=True)
print data['col1']    # Output: array([   1.,   10.,  100.])
print data['col2']    # Output: array([   2.,   20.,  200.])
print data['col3']    # Output: array([   3.,   30.,  300.])

Since in this case the data is formed like this

row1,   1,  10, 100
row2,   2,  20, 200
row3,   3,  30, 300

you can achieve something similar using the following code snippet:

labels = np.genfromtxt('data.txt', delimiter=',', usecols=0, dtype=str)
raw_data = np.genfromtxt('data.txt', delimiter=',')[:,1:]

The first line reads the first column (the labels) into an array of strings. The second line reads all data from the file but discards the first column. This is how you can extract columns in different variables.

if you want to plot col1 and col2, you can simply define:

x=data['col1']
y=data['col2']
Sayali Sonawane
  • 12,289
  • 5
  • 46
  • 47
0
from matplotlib import pyplot as plt
from matplotlib import style
import numpy as np
import datetime as dt
from datetime import datetime
import matplotlib.dates as mdates


style.use('ggplot')
data = np.genfromtxt('I112-1.csv', unpack=True,dtype=None, names=True,delimiter = ',', converters={0: lambda x: datetime.strptime(x, '%d-%m-%Y %H:%M')})
x = data['Time']
y = data['Charge']

plt.plot (x,y)                
plt.title('Panel Charge')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
warrenfitzhenry
  • 2,209
  • 8
  • 34
  • 56