CSVファイルからデータを読み込みグラフの画像を作成
# -*- coding: utf-8 -*-
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import csv
import sys
from dateutil.parser import parse
#CSVファイルからデータを取得
csv_file_list = [CSVファイル名1,CSVファイル名2]
for csv_file in csv_file_list:
print cache
#CSV読み込み
csv_reader = csv.reader(open('csv/' + csv_file + '.csv'))
# x軸のデータ
x = []
# y軸のデータ
y = []
cnt = 0
for row in csv_reader:
# 1行目はヘッダー
if cnt > 0:
#x軸データを追加
x.append(parse(row[0]))
#y軸データを追加
y.append(row[1])
cnt = cnt + 1
# データをセット
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)
# グラフのフォーマットの設定
days = mdates.HourLocator(range(0,60,1))
daysFmt = mdates.DateFormatter('%H:%M')
ax.set_title('タイトル')
ax.set_xlabel('datetime')
ax.set_ylabel('count')
ax.xaxis.set_major_locator(days)
ax.xaxis.set_major_formatter(daysFmt)
fig.autofmt_xdate()
# HTMLに埋め込むためにファイル出力
plt.savefig('test.png')
◇おまけ
線の色の設定方法はこちら
plot(x+1, x, "b") # 青
plot(x+2, x, "g") # 緑
plot(x+3, x, "r") # 赤
plot(x+4, x, "c") # シアン
plot(x+5, x, "m") # マゼンタ
plot(x+6, x, "y") # 黄
plot(x+7, x, "k") # 黒
plot(x+8, x, "w") # 白
※参考URL