使用rpy2设置晶格打印选项时出现问题

2024-09-29 21:48:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用numpy数组中的数据,使用rpy2和lattice来创建热图或颜色强度图。我使用的是python2.6.2、r2.10.1、rpy2.1.9,不确定是哪个版本的lattice。我已经让它工作得很好了,除了我需要修改用于绘制相关变量(z)级别的颜色渐变的默认晶格设置。具体地说,我想要灰度而不是品红色青色的默认渐变。下面是生成虚拟数据帧并在vanillar R中创建灰度级图的代码:

library(lattice)

x <- rep(seq(1,10), each=10)
y <- rep(seq(1,10), 10)
z <- abs(rnorm(100))
z <- z/max(z)
df <- data.frame(x=x, y=y, z=z)

grayvector <- gray(seq(0,1,1/100))

foo <- levelplot(z ~ x * y, data=df, col.regions = grayvector)
print foo

使用rpy2时,我无法设置col.地区争论。根据文档,rpy2应该转换任何。“”的函数参数中的字符。然而,这似乎行不通,因为使用col_nu区域会导致参数被忽略。以下是生成levelplot的python代码,但没有灰度:

^{pr2}$

有人知道如何将格函数参数与a一起使用吗。在rpy2里?在


Tags: 数据代码dfdatafoo颜色col函数参数
1条回答
网友
1楼 · 发布于 2024-09-29 21:48:37

需要手动指定参数映射:

from rpy2.robjects.functions import SignatureTranslatedFunction
lattice = importr("lattice")
lattice.levelplot = SignatureTranslatedFunction(lattice.levelplot,
                                                init_prm_translate={'col_regions': 'col.regions'})
foo = lattice.levelplot(formula, data=df, col_regions=grayvector)

{同时检查}

It is important to understand that the translation is done by inspecting the signature of the R function, and that not much can be guessed from the R ellipsis ‘...’ whenever present.

相关问题 更多 >

    热门问题