2014-08-19 5 views
3

텍스트 파일에서 타임 스탬프 (형식 : %Y-%M-%D %H:%M:%S)가 수집 된 배열이 있습니다. matplotlib을 사용하여 이들을 플롯에 그리려고합니다. 그러나 나는 그것을 작동시킬 수 없다.matplotlib.date2num에 타임 스탬프가 전달되었습니다. 'str'객체에 'toordinal'속성이 없습니다.

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

를하지만 나는 다음과 같은 오류가 작동하지 않는 : 나는이 생각하고 있었는데 'str' object has no attribute 'toordinal' 라인 (18) (md.date2num(mydata['timestamp'에 맞춰)

데이터 샘플에서 :의

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 
+2

은 내가 당신의 날짜 계산기가 실제로 작동하고 있다고 생각하지 않습니다. 컬럼은'datetime' 오브젝트가 아닌 문자열 인 것처럼 보입니다. 샘플 데이터를 게시 할 수 있습니까? (5-10 줄이면 충분합니다.) –

답변

2

우선 모든 형식 문자열이 잘못되었습니다. 봐 : http://strftime.org/

%M Minute as a zero-padded decimal number.

%의 D이 전혀 존재입니다!

두 번째로 .date2num을 사용하십니까? o_0 왜 그것들을 보통 datetime 객체로 저장하지 않고 단지 format the ticks을 원하는대로 저장합니까?

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

관련 문제