2010 年 9 月平均気温
| area | temperature |
|---|---|
| Tokyo | 25.1 |
| Abashiri | 17.8 |
| Naha | 28.0 |
# -*- coding:utf-8 -*-
from numpy import *
import Gnuplot, Gnuplot.funcutils
import os
import time
import MySQLdb
########################################################################
# @brief グラフを描く
########################################################################
def drawGraph(filename, pngfile, xmin, xmax, ymin, ymax):
g = Gnuplot.Gnuplot(debug=1) # debug=1 を引数に付けるとコンソールに gnuplot に渡したコマンドが表示される
# Gnuplot に指示を与える
g('set xrange [%d:%d]' % (xmin, xmax))
g('set yrange [%d:%d]' % (ymin, ymax))
g('set boxwidth 0.5 relative')
g('set style fill solid 0.2')
g('set grid')
g('unset key') # 凡例なし
# データ読み込み
str = "'%s' using 2:xticlabels(1) with boxes" % (filename)
g.plot(str)
# こちらでも png 出力可能, 一瞬 window が表示される
g.hardcopy(pngfile, terminal = 'png')
# すぐに終了してしまうとデータ出力できないので 0.1 sec sleep
time.sleep(0.1)
########################################################################
# @brief Main function
########################################################################
# temporary ディレクトリを current directory にする
os.environ['TEMP']="."
# DB に接続
db = MySQLdb.connect(user="username", passwd="password", db="test", charset="utf8")
# SELECT
db.query("SELECT `area`, `temperature` FROM `temperature`;")
result = db.store_result()
# データ格納
filename = 'data03.dat'
pngfile = 'date03.png'
f = open(filename, 'w')
num = result.num_rows()
for row in result.fetch_row(num):
str = "%s\t%.2f\n" % (row[0], row[1])
# print str
f.write(str)
f.close()
# グラフ作成
drawGraph(filename, pngfile, -1, 3, 0, 30)
0 件のコメント:
コメントを投稿