Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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
                i don't think your date converter is actually working. the column appears to be still be a string not a datetime object. can you post some sample data? (5 - 10 lines should suffice)
– Paul H
                Aug 19, 2014 at 19:39

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()
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.