Created
December 21, 2016 01:44
-
-
Save JuYoungAhn/efbbdc44d23b42d9b76cd1cdcaef48ad to your computer and use it in GitHub Desktop.
foobot data visualization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# In[43]: | |
""" | |
최종수정 : 2016/12/7 | |
""" | |
# 라이브러리 로드 | |
get_ipython().magic(u'matplotlib inline') | |
import matplotlib | |
import matplotlib.ticker as ticker | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import matplotlib.dates as md | |
import datetime as dt | |
def preprocess(df, color, label) : | |
""" | |
pandas read_csv로 읽은 데이터를 전처리한다. | |
json 파일을 csv 형식으로 변형하여 다운받은 경우 | |
Args: | |
df : 로우 데이터 | |
color : 데이터 플롯시 라인 색깔 | |
label : 해당 데이터 label | |
Returns: | |
전처리가 완료된 pandas DataFrame | |
""" | |
# 컬럼 지정 | |
# 차례대로 timestamp, particular matter, temperature, humadity, co2, voc, global(combination of vod and pm) | |
df.columns = ['time', 'pm', 'tmp', 'hum', 'co2', 'voc' , 'global'] | |
dates = [dt.datetime.fromtimestamp(ts) for ts in df.time] # 타임 스탬프를 date 타입으로 변경 | |
datenums = md.date2num(dates) # date 를 숫자 타입으로 변경 (x축에 플롯 하기 위해) | |
df['date'] = dates # date : 측정 시간 (date 타입) | |
df['date2'] = datenums # date2 : 측정 시간 (num 타입) | |
""" 플롯을 위한 설정 값 """ | |
df.color = color # 라인 플롯의 색깔 지정 | |
df.label = label # 해당 데이터의 label 값 지정 | |
return df | |
def preprocess2(df, color, label) : | |
""" | |
pandas read_csv로 읽은 데이터를 전처리한다. | |
foobot 홈페이지에서 직접 다운로드 받은 경우 | |
Args: | |
df : 로우 데이터 | |
color : 데이터 플롯시 라인 색깔 | |
label : 해당 데이터 label | |
Returns: | |
전처리가 완료된 pandas DataFrame | |
""" | |
# 컬럼 지정 | |
# 차례대로 timestamp, particular matter, temperature, humadity, co2, voc, global(combination of vod and pm) | |
df.columns = ['time', 'local_time', 'pm', 'tmp', 'hum', 'co2', 'voc' , 'global'] | |
dates = [dt.datetime.fromtimestamp(ts) for ts in df.time] # 타임 스탬프를 date 타입으로 변경 | |
datenums = md.date2num(dates) # date 를 숫자 타입으로 변경 (x축에 플롯 하기 위해) | |
df['date'] = dates # date : 측정 시간 (date 타입) | |
df['date2'] = datenums # date2 : 측정 시간 (num 타입) | |
""" 플롯을 위한 설정 값 """ | |
df.color = color # 라인 플롯의 색깔 지정 | |
df.label = label # 해당 데이터의 label 값 지정 | |
return df | |
def plot(df_list, title, tick=0.25, ylim=None) : | |
""" | |
전처리된 데이터 리스트를 받아 콘솔창에 플롯한다. | |
Args: | |
df_list : 전처리된 foobot 데이터 리스트 | |
title : 해당 플롯의 타이틀 | |
tick : x축 간격. 작을수록 촘촘하게 출력됨 | |
ylim : y축의 제한을 원하는 경우 제한 값 지정 | |
Returns: | |
콘솔창에 플롯됨 | |
""" | |
# 폰트 스타일 지정 | |
font = {'family' : 'normal', | |
'weight' : 'normal', | |
'size' : 21} | |
plt.rc('font', **font) | |
# 플롯 사이즈 지정 | |
plt.figure(figsize=(25,15)) | |
# x축을 반시계 방향으로 돌림 | |
plt.xticks( rotation=25 ) | |
# x축 date의 형식 지정 : 월-일 시간-분 ex) 12-02 12:00 | |
ax=plt.gca() | |
xfmt = md.DateFormatter('%m-%d %H:%M') | |
ax.xaxis.set_major_formatter(xfmt) | |
# 수평 라인을 그음 | |
plt.axhline(y=300, linewidth=2.0, color = 'black') | |
plt.axhline(y=400, linewidth=2.0, color = 'red') | |
# x축 간격 지정 | |
tick_spacing = tick | |
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing)) | |
# 리스트를 돌면서 플롯을 함 | |
for df in df_list : | |
plt.plot(df['date2'], df['voc'], label=df.label, color=df.color) | |
# title을 지정 | |
plt.title(title, fontsize=40) | |
# x,y label을 지정 | |
plt.xlabel("time(s)", fontsize=30) | |
plt.ylabel("voc(ppb)", fontsize=30) | |
# 범례 위치, 모양 설정 | |
leg = plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.15), fancybox=True, shadow=True, ncol=7, fontsize=25) | |
# 범례에서 보이는 라인의 굵기 설정 | |
for legobj in leg.legendHandles: | |
legobj.set_linewidth(5.0) | |
# y축 제한이 있는 경우 y축의 제한 설정 | |
if ylim != None : | |
plt.ylim((0,ylim)) | |
# plot을 화면에 출력함 | |
plt.show() | |
# In[44]: | |
# 파일 이름의 suffix 지정 | |
file_name = '2016-12-02to2016-12-07.csv' | |
# 플롯 할 때의 title 지정 | |
title = '[2016/12/02 0:00 ~ 2016/12/07 16:00]' | |
# line 색깔 지정, 잘 보이지 않는 경우 잘 보이는 값으로 설정 가능 | |
colors = ['blue', 'purple', 'black', 'brown', 'crimson', 'darkgray', 'darkred', 'darksalmon', | |
'indigo', 'hotpink', 'maroon', 'lime', 'mediumblue', 'olive','navy', 'sienna', 'skyblue', | |
'teal', 'palegreen', 'yellowgreen'] | |
""" 데이터를 읽은 후 전처리하여 pandas DataFrame 타입에 저장 """ | |
foobot00 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot00-'+file_name), color=colors[0], label='foobot00') | |
foobot01 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot01-'+file_name), color=colors[1], label='foobot01') | |
foobot02 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot02-'+file_name), color=colors[2], label='foobot02') | |
foobot03 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot03-'+file_name), color=colors[3], label='foobot03') | |
# foobot04 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot04-'+file_name), color=colors.next(), label='foobot04') | |
foobot05 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot05-'+file_name), color=colors[5], label='foobot05') | |
foobot06 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot06-'+file_name), color=colors[6], label='foobot06') | |
foobot07 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot07-'+file_name), color=colors[7], label='foobot07') | |
foobot08 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot08-'+file_name), color=colors[8], label='foobot08') | |
foobot09 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot09-'+file_name), color=colors[9], label='foobot09') | |
foobot10 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot10-'+file_name), color=colors[10], label='foobot10') | |
foobot11 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot-11-'+file_name), color=colors[11], label='foobot11') | |
foobot12 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot12-'+file_name), color=colors[12], label='foobot12') | |
foobot15 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot15-'+file_name), color=colors[15], label='foobot15') | |
foobot16 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot16-'+file_name), color=colors[16], label='foobot16') | |
foobot17 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot17-'+file_name), color=colors[17], label='foobot17') | |
foobot18 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot18-'+file_name), color=colors[18], label='foobot18') | |
# In[45]: | |
""" 전후 데이터 비교 """ | |
print "전처리 되기 전 데이터" | |
print pd.read_csv('/Users/ahn/downloads/foobot/Yoo-Foobot00-'+file_name).head() | |
print "전처리 된 후 데이터" | |
print foobot00.head() | |
# In[46]: | |
plot([foobot00, foobot01, foobot02, foobot03, foobot05, foobot06, foobot07] | |
,title = "Foobot0,1,2,3,4,5,6,7 "+title) | |
# In[47]: | |
plot([foobot08, foobot09, foobot10] | |
,title = "Foobot8,9,10 "+title) | |
# In[48]: | |
plot([foobot11, foobot12] | |
,title = "Foobot11,12"+title) | |
# In[49]: | |
plot([foobot15, foobot16, foobot17, foobot18] | |
,title = "Foobot15,16,17,18"+title) | |
# In[50]: | |
# 예제코드1 한 개 파일을 읽은 후 Plot | |
import pandas as pd | |
foobot00 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot00-2016-12-02to2016-12-07.csv'), color="blue", label="foobot00") | |
plot([foobot00], title="foobot00") | |
# In[51]: | |
# 예제코드2 여러개를 동시에 plot 하고 싶은 경우 | |
import pandas as pd | |
foobot00 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot00-2016-12-02to2016-12-07.csv'), color="blue", label="foobot00") | |
foobot01 = preprocess(pd.read_csv('data/foobot/Yoo-Foobot01-2016-12-02to2016-12-07.csv'), color="red", label="foobot01") | |
plot([foobot00, foobot01], title="foobot00, 01") | |
# In[52]: | |
# 예제코드3 파일이 Foobot 홈페이지에서 직접 다운로드 받은 파일일 경우 | |
foobot_test = preprocess2(pd.read_csv('data/foobot/foobot_data_from_website.csv'), color='blue', label='test') | |
plot([foobot_test], title='foobot data from website', tick=1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment