How to use the variable from the python in rpy2? [closed]

2024/11/20 16:17:01

My simple program extracts the database from Python and store in the variable row.

cursor = con.cursor()       cursor.execute("SELECT * FROM traffic")#Retrieves data from SQLrows = cursor.fetchall()  for row in rows:row = list(row)a = row[1:]b = row[:-1]print(a)print(b)

Now that I am able to get the month and traffic in list a and b like [1000L]

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

Now I want to plot, histogram and piechart out of it. The row contains two coloumns: MOnth and Traffic. I want to convert this into the chart using Rpy2. How do I do it? Here's my table:

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

First, make two lists from your database. Something like:

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'])

Then, now you have two python lists, you can make a plot thus:

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

You maybe want lines:

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

You maybe want nice labels:

>>> r.plot(Month,Traffic,type="l",xlab="Month",ylab="Traffic")
https://en.xdnf.cn/q/120762.html

Related Q&A

Pandas function operations [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed l…

What is the use of iter in python? [duplicate]

This question already has answers here:Understanding for loops in Python(4 answers)Closed last month.What is the use of using the iter function in python?Instead of doing:for i in range(8):print iI co…

python calculator with two float numbers as parameters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 7 years ago.Improve…

python - Is it possible to combine 2 functions together to create 1 function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.Improv…

How to create a grading system in Python? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python lists comprehension [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 1…

Import everyday date for one year

Is there a simple way to import into my Python file a list with all the dates for a given year? What I need is to store everyday that a year has (01.01.2018----31.12.2018) into a list, so I can then i…

Object detection realtime using tensorflow

Im trying to detect objects in realtime using tensorflow. . I ran jupyter notebook in object_detection directory. then I opened the notebook file. It is firing the following errorIm getting the followi…

How do I get Python to read and extract words from a text file? [duplicate]

This question already has answers here:How to copy files(21 answers)Closed 7 years ago.So I need to make a code that opens a txt file, and then takes the content of that file and puts it into another t…

How to draw a checkered flag to the Python screen?

QUESTION: Implement the following pseudocode to draw a checkered flag to the screen.1. Ask the user for the size of the checkered flag (n). 2. Draw an n x n grid to the screen. 3. For i = 0,2,4,...,…