Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Have an array with timestamps (format %Y-%M-%D %H:%M:%S) collected from a textfile. I want to plot these in a subplot with matplotlib. But I can't get it to work. I was thinking of this:

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 is not working I get the following error: 'str' object has no attribute 'toordinal' at line 18 (line with md.date2num(mydata['timestamp')

Data sample:

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
like image 1000
SjonTeflon Avatar asked Oct 31 '22 18:10

SjonTeflon


1 Answers

First of all your format string is wrong. Look: http://strftime.org/

%M Minute as a zero-padded decimal number.

and %D is not exists at all!

Secondly, why do you use .date2num? o_0 Why do not store them as normal datetime objects instead and just format the ticks as you want?

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

like image 140
Bruno Gelb Avatar answered Nov 15 '22 05:11

Bruno Gelb