python从数据库值绘制图表

时间:2014-11-13 13:56:30

标签: python mysql matplotlib

我有一个数据库,它由下面提供的值组成。我需要访问这些值并绘制图表,但我得到错误:

 File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 2430, in pie
    assert(len(x) == len(labels))
AssertionError

我的数据库表:a1

no a  b
1  ab 24
2  ac 25
3  ad 26
4  ae 27
5  ag 28

我的代码:请帮我纠正我的代码!

import MySQLdb

db = MySQLdb.connect(host="localhost", # your host, usually localhost
                     user="root", # your username
                      passwd="mysql", # your password
                      db="sakila") # name of the data base
cursor = db.cursor()

# execute SQL select statement
cursor.execute("SELECT b from a1")


# commit your changes
db.commit()

keywords=[]
#here fetchall() gets all the rows and we append carnames to key words


for i in cursor.fetchall():
    keywords.append(i[0])

from pylab import *

# make a square figure and axes
figure(1, figsize=(6,6))
ax = axes([0.1, 0.1, 0.8, 0.8])

# The slices will be ordered and plotted counter-clockwise.
labels = ['Frogs', 'Hogs', 'Dogs', 'Logs','togs']
fracs = [keywords]
explode=(0, 0.05, 0, 0,0)

pie(fracs, explode=explode, labels=labels,
                autopct='%1.1f%%', shadow=True, startangle=90)
                # The default startangle is 0, which would start
                # the Frogs slice on the x-axis.  With startangle=90,
                # everything is rotated counter-clockwise by 90 degrees,
                # so the plotting starts on the positive y-axis.

title('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5})

show()

答案将不胜感激!

1 个答案:

答案 0 :(得分:0)

只是一个简单的修复!

fracs=keywords