我做了这个Python脚本。这个想法是使用这个Python脚本,这样matplotlib就可以使用MySQL数据库中的数据为我生成一个垂直条形图。我想做的另一件事是保存png文件而不是显示它(我试图通过putty运行这段代码),我认为我做得很正确。当我运行代码时,我遇到了一些错误,我不知道这意味着什么。希望有人能指出我哪里做错了。在#!/usr/bin/python
# matplotlib pyplot module
import MySQLdb
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
# connect to MySQL database
conn = MySQLdb.connect(host="localhost", user="xx", passwd="xxxx", db="r1array")
# prepare a cursor
cur = conn.cursor()
# this is the query we will be making
query = """
SELECT TimeStamp,Pac
FROM SolarData
WHERE TimeStamp >= "2014-01-03"
AND TimeStamp < "2014-01-04";
"""
# execute the query
cur.execute(query)
# retrieve the whole result set
data = cur.fetchall()
# close cursor and connection
cur.close()
conn.close()
# unpack data in TimeStamp (x axis) and Pac (y axis)
TimeStamp, Pac = zip(*data)
# graph code
plt.bar(TimeStamp, Pac, align='center')
# set title, X/Y labels
plt.title("PVIC R1 panel")
plt.xlabel("Time of Day")
plt.ylabel("Pac")
fig = plt.gcf()
# plt.xticks(TimeStamp, (hour))
fig.set_size_inches(20.5,10.5)
plt.grid(True)
plt.draw()
fig.savefig('test2.png', dpi=100)
我收到的错误信息是
^{pr2}$
尤其是最后一行,我不明白。在ValueError: ordinal must be >= 1
这是我从MySQL数据库调用的数据。在TimeStamp | Pac |
+---------------------+------+
| 2014-01-03 07:55:02 | 0 |
| 2014-01-03 08:00:02 | 0 |
| 2014-01-03 08:05:02 | 2 |
| 2014-01-03 08:10:02 | 11 |
| 2014-01-03 08:15:03 | 72 |
| 2014-01-03 08:20:04 | 162 |
| 2014-01-03 08:25:04 | 307 |
| 2014-01-03 08:30:04 | 464 |
| 2014-01-03 08:40:01 | 665 |
| 2014-01-03 08:45:01 | 764 |
| 2014-01-03 08:50:02 | 844 |
| 2014-01-03 08:55:01 | 931 |
| 2014-01-03 09:00:02 | 1017 |
| 2014-01-03 09:05:02 | 1098 |
| 2014-01-03 09:10:01 | 1176 |
| 2014-01-03 09:15:02 | 1272 |
| 2014-01-03 09:20:01 | 1351 |
| 2014-01-03 09:25:02 | 1432 |
| 2014-01-03 09:30:01 | 1528 |
| 2014-01-03 09:35:02 | 1653 |
| 2014-01-03 09:40:02 | 1738 |
| 2014-01-03 09:45:01 | 1821 |
| 2014-01-03 09:50:02 | 1895 |
| 2014-01-03 09:55:01 | 2015 |
| 2014-01-03 10:00:02 | 2153 |
| 2014-01-03 10:05:01 | 2300 |
| 2014-01-03 10:10:02 | 2482 |
| 2014-01-03 10:15:02 | 2635 |
| 2014-01-03 10:20:01 | 2750 |
| 2014-01-03 10:25:02 | 2923 |
| 2014-01-03 10:30:01 | 3059 |
| 2014-01-03 10:35:02 | 3201 |
| 2014-01-03 10:40:01 | 3275 |
| 2014-01-03 10:45:02 | 3360 |
我需要的是条形图。现在我能得到一个图像,但这只是一个错误的图像。