Timestamps passed to matplotlib.date2num: object 'str' has no attribute 'toordinal'

Have an array with timestamps (format %Y-%M-%D %H:%M:%S

) gathered from a text file. I want to plot them in subplot with matplotlib. But I cannot get it to work. I thought about it:

import numpy as np
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as md

dateconv = lambda s: datetime.strptime(s, '%Y-%M-%D %H:%M:%S:.%f')

col_names = ["timestamp", "light", "sensor1", "sensor2", "sensor3", "temp"]
dtypes = ["object", "uint8", "uint8", "uint8", "uint8", "float"]
mydata = np.genfromtxt("data.csv", delimiter=",", names = col_names, dtype=dtypes, converters={"Time": dateconv})


time = md.date2num(mydata['timestamp'])
sensor1 = mydata['sensor1']
sensor2 = mydata['sensor2']
sensor3 = mydata['sensor3']
light = mydata['light']
temp = mydata['temp']

fig = plt.figure()
rect = fig.patch
rect.set_facecolor('#31312e')

ax1 = fig.add_subplot(3,2,1, axisbg='grey')
ax1.plot_date(time, sensor1, 'c', linewidth=2)
ax1.tick_params(axis='x', colors='c')
ax1.tick_params(axis='y', colors='c')
ax1.spines['bottom'].set_color('w')
ax1.spines['top'].set_color('w')
ax1.spines['left'].set_color('w')
ax1.spines['right'].set_color('w')
ax1.yaxis.label.set_color('c')
ax1.xaxis.label.set_color('c')
ax1.set_title('Sensor 1', color = 'c')
ax1.set_xlabel('Time')
ax1.set_ylabel('Value')
ax1.set_ylim(0, 255)

ax2 = fig.add_subplot(3,2,2, axisbg='grey')
#so on...

plt.setp(ax1.xaxis.get_majorticklabels(), rotation = 25)
plt.show()

      

But it doesn't work. I am getting the following error: 'str' object has no attribute 'toordinal'

on line 18 (line c md.date2num(mydata['timestamp'

)

Sample data:

2014-08-12 22:45:12.826871, 65, 244, 213, 196, 21.625
2014-08-12 22:50:14.151601, 66, 246, 208, 196, 21.312
2014-08-12 22:55:15.399692, 15, 247, 208, 196, 21.375
2014-08-12 23:00:16.717546, 15, 248, 209, 195, 21.5
2014-08-12 23:05:18.041433, 15, 249, 212, 195, 21.625
2014-08-12 23:10:19.372733, 16, 248, 216, 195, 21.687

      

+4


source to share


1 answer


First of all, your format string is wrong. Take a look: http://strftime.org/

% M Minute as a zero-padded decimal number.

and % D doesn't exist at all!



Second, why are you using .date2num ? o_0 Why not save them as regular datetime objects and just format the ticks as you wish?

import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime

time_format = '%Y-%m-%d %H:%M:%S.%f'

col_names = ["timestamp", "light", "sensor1", "sensor2", "sensor3", "temp"]
dtypes = ["object", "uint8", "uint8", "uint8", "uint8", "float"]
mydata = np.genfromtxt("data.csv", delimiter=",", names=col_names, dtype=dtypes)

time = [datetime.strptime(i, time_format) for i in mydata['timestamp']]
sensor1 = mydata['sensor1']

fig = plt.figure()
rect = fig.patch
rect.set_facecolor('#31312e')

ax1 = fig.add_subplot(3, 2, 1, axisbg='grey')
ax1.plot_date(time, sensor1, 'c', linewidth=2)
ax1.tick_params(axis='x', colors='c')
ax1.tick_params(axis='y', colors='c')
ax1.spines['bottom'].set_color('w')
ax1.spines['top'].set_color('w')
ax1.spines['left'].set_color('w')
ax1.spines['right'].set_color('w')
ax1.yaxis.label.set_color('c')
ax1.xaxis.label.set_color('c')
ax1.set_title('Sensor 1', color='c')
ax1.set_xlabel('Time')
ax1.set_ylabel('Value')
ax1.set_ylim(0, 255)

ax2 = fig.add_subplot(3, 2, 2, axisbg='grey')
# so on...

plt.setp(ax1.xaxis.get_majorticklabels(), rotation=25)
plt.show()

      

enter image description here

+3


source







All Articles