【问题标题】:How to plot using data in database in Rpy2?如何在 Rpy2 中使用数据库中的数据进行绘图?
【发布时间】:2012-08-15 11:41:42
【问题描述】:

数据存储在一个名为“data”的数据库中,其中包含“month”和“traffic”两列。该表如下所示:

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

现在我已经使用 python 从数据库中提取了数据。我希望使用这些数据进行绘图。这是我的代码:

#usr/bin/python

import MySQLdb as mdb
import rpy2.robjects as robjects
r = robjects.r

def database():
    """Retrieves the data from the database"""

    #Database Connection
    con = mdb.connect('localhost', 'root', 'devil', 'data');

    with con:
        #Submit statements to SQL server
        cursor = con.cursor()       
        cursor.execute("SELECT * FROM traffic")

        #Retrieves data from SQL
        rows = cursor.fetchall()  
        for row in rows:
            row = list(row)
            x = row[1:]
            y = row[:-1]
            r.plot(x, y)


database()

我想使用变量 x 和 y 进行绘图。 r.plot(x, y) 不工作。这是错误:

 res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in plot.window(...) : need finite 'ylim' values

如何绘制?

【问题讨论】:

  • 你忘了问问题。
  • xy 中有什么内容?看起来任何一个变量都是相同数字的列表...
  • x 保存流量,y 保存月份。
  • 部分问题在于 Month 是一个字符变量。

标签: python r plot rpy2


【解决方案1】:

忘记循环。看看什么是“行”。它是数据库行值的元组列表。大概。打印出来,告诉我们什么是“行”。

然后您只需将这些元组解包到列表 x 和 y 中,将月份映射到整数或有序因子,然后调用 r.plot(x,y)。

【讨论】:

    猜你喜欢
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 2013-01-17
    • 2012-05-07
    • 2011-09-24
    相关资源
    最近更新 更多