Python 如何在Rpy2中使用数据库中的数据进行绘图?
数据存储在一个名为“数据”的数据库中,其中有两个冒号“月”和“流量”。该表如下所示:Python 如何在Rpy2中使用数据库中的数据进行绘图?,python,r,plot,rpy2,Python,R,Plot,Rpy2,数据存储在一个名为“数据”的数据库中,其中有两个冒号“月”和“流量”。该表如下所示: +-----------+---------+ | month | traffic | +-----------+---------+ | January | 1000 | | February | 100 | | March | 10430 | | April | 1500 | | May | 100 | | June |
+-----------+---------+
| 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、 绘图(x,y)不工作。这就是错误:
res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in plot.window(...) : need finite 'ylim' values
如何绘图?忘记循环。看看“行”是什么。它是数据库行值的元组列表。可能打印出来,告诉我们“行”是什么
然后你只需要将这些元组展开到列表x和y中,将月份映射到整数或有序因子,然后调用r.plot(x,y)。你忘了问一个问题。
x
和y
中有什么?看起来两个变量中的任何一个都是相同数字的列表…x代表流量,y代表月份。部分问题的可能重复之处在于,月份是一个字符变量。