rpy2 Scatter plot

rpy2で散布図を書く

import rpy2.robjects as robjects
from rpy2.robjects.packages import importr

grdevices = importr('grDevices')
grdevices.png(file="file.png", width=512, height=512)
# plotting code here

# 月齢
lstx = [0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,
        10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0,
        20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0]

# 漁獲高
lsty = [190.0, 200.0, 50.0, 1700.0, 360.0, 620.0, 450.0, 2100.0,
        120.0, 5000.0, 1900.0, 780.0, 880.0, 1500.0, 1500.0, 1900.0,
        200.0, 270.0, 2100.0, 7000.0, 900.0, 1200.0, 3700.0, 2000.0,
        2900.0, 1300.0, 140.0, 2250.0, 120.0, 1000.0]

rx = robjects.FloatVector(lstx)
ry = robjects.FloatVector(lsty)

robjects.r.plot(x=rx, y=ry, xlab="moon age", ylab="fish", col="blue")

grdevices.dev_off()

file
csvファイルから読み込む場合は、Rの命令文をそのまま書いてもよい

import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
grdevices = importr('grDevices')
grdevices.png(file="file.png", width=512, height=512)

robjects.r('''
data <-read.csv('fish_moonage.csv', header=TRUE)
plot(fish~moonage, xlab="foo", data=data, col="purple")
''')

grdevices.dev_off()
Posted: August 27th, 2010 | Author: | Filed under: 技術 | Tags: , , , , | No Comments »

Leave a Reply