rpy2 plot问题(设备没有响应)rpy2.rinterface.NULL

问题描述 投票:0回答:1
Setup: 
1. win10,
2. python3.5/3.6, 
3. R 3.42,
4. rpy2 2.90 or 2.8x
5. Ipython 6.1.0

当我使用rpy2作为rpy2文档时,绘图存在问题:

In [26]: import rpy2.robjects as robjects
...:
...: r = robjects.r
...:
...: x = robjects.IntVector(range(10))
...: y = r.rnorm(10)
...:
...: r.X11()
...:
...: r.layout(r.matrix(robjects.IntVector([1,2,3,2]), nrow=2, ncol=2))
...: r.plot(r.runif(10), y, xlab="runif", ylab="foo/bar", col="red")
...:
Out[26]: rpy2.rinterface.NULL

在绘图后有一个图形,但图形有问题,标题变为:R图形:Device3(ACTIVE)(没有响应),然后python崩溃,我需要重新启动它。

because my system language is chinese, the tranlation of the picture title is R Graphics: Device3 (ACTIVE) (not responding)

我尝试了不同版本的rpy2或python,他们也有这个问题。

谁能帮我?

plot graphics rpy2
1个回答
0
投票

尝试以下解决方法,即使在2019年似乎也需要它。如果在预览中几乎没有显示图表并且单击时窗口没有反应:加载图表两次并使用r.windows()/ r.X11()/ r.quartz()介于两者之间(图形命令Windows / Unix / Mac根据https://www.statmethods.net/graphs/creating.html - >查看多个图形,但至少X11()似乎也适用于Windows)。然后使用grdevices.dev_off()两次并选择中间的等待时间或仅执行一次,并使用隔离的grdevices.dev_off()语句将其关闭。

import time
from rpy2.robjects.packages import importr
from rpy2.robjects import r
from rpy2.robjects.lib import ggplot2
grdevices = importr('grDevices')
rprint = robjects.globalenv.get("print")

pp = ggplot2.ggplot(mtcars) + \
     ggplot2.aes_string(x='wt', y='mpg', col='factor(cyl)') + \
     ggplot2.geom_point() + \
     ggplot2.geom_smooth(ggplot2.aes_string(group = 'cyl'), method = 'lm')

rprint(pp)
r.windows()
rprint(pp)
grdevices.dev_off()

#From here optional, if you want a waiting time
#Elsewise close the plot manually afterwards with grdevices.dev_off()
time.sleep(10)
grdevices.dev_off()

如果窗口仍然崩溃,执行grdevices.dev_off(),不需要强行关闭

替代方案:根本不显示情节,而是保存图片。

grdevices.dev_copy(device = r.png, filename = "plot.png", width = 1000, height = 500)
rprint(pp)
grdevices.dev_off()
© www.soinside.com 2019 - 2024. All rights reserved.