-21

我的简单程序从 Python 中提取数据库并存储在变量行中。

cursor = con.cursor()       
    cursor.execute("SELECT * FROM traffic")

    #Retrieves data from SQL
    rows = cursor.fetchall()  

    for row in rows:
       row = list(row)
       a = row[1:]
       b = row[:-1]
       print(a)
       print(b)

现在我能够获得列表 a 和 b 中的月份和流量,例如 [1000L]

['January']
[100L]
['February']
[10430L]
['March']
[1500L]
['April']
[100L]
['May']
[1200L]
['June']
[800L]
['July']
[8000L]
['August']
[100000L]
['September']

现在我想绘制它,直方图和饼图。该行包含两个列:MOnthTraffic。我想使用 Rpy2 将其转换为图表。我该怎么做?这是我的桌子:

month     | traffic |
+-----------+---------+
| January   |    1000 |
| February  |     100 |
| March     |   10430 |
| April     |    1500 |
| May       |     100 |
| June      |    1200 |
| July      |     800 |
| August    |    8000 |
| September |  100000 |
+-----------+---------+
4

1 回答 1

1

首先,从您的数据库中创建两个列表。就像是:

cursor = con.cursor()       
cursor.execute("SELECT * FROM traffic")

#Retrieves data from SQL
rows = cursor.fetchall()  

Month = list()
Traffic = list()

for row in rows:
    Month.append(row['Month'])          # guesswork - what does a row look like?
    Traffic.append(row['Traffic'])

然后,现在你有两个 python 列表,你可以制作一个图:

>>> r.plot(Month,Traffic)
rpy2.rinterface.NULL

你可能想要线条:

>>> r.plot(Month,Traffic,type="l")
rpy2.rinterface.NULL

您可能想要漂亮的标签:

>>> r.plot(Month,Traffic,type="l",xlab="Month",ylab="Traffic")
于 2012-08-16T10:55:49.773 回答