简体   繁体   English

为什么在使用rpy2的Mac中无法正确显示情节?

[英]why plot is not shown properly in Mac when using rpy2?

I'm trying to make R plot in Python via rpy2 in Mac, but the plot is not shown up completely or python crashes after I close the window. 我正在尝试通过Mac中的rpy2在Python中绘制R图,但是在关闭窗口后该图未完全显示出来或python崩溃。

If I run the codes below without r.quartz() , there will be no graph at all but there is a pdf file named 'Rplot' generated in my folder which cannot be opened. 如果我在不使用 r.quartz() 情况下运行以下代码,则将完全没有图形,但是在我的文件夹中生成了一个名为“ Rplot”的pdf文件,无法打开。

If I run the codes below with r.quartz() , plot will show up but python will quit unexpectedly after I close the graph window. 如果我使用 r.quartz()运行以下代码,则将显示图,但在关闭图形窗口后python会意外退出。 I tried the codes in Windows and they work well. 我在Windows中尝试了代码,它们运行良好。

import rpy2.robjects as robjects

r = robjects.r

x = robjects.IntVector(range(10))

y = r.rnorm(10)

r.quartz()

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")

Depending on your local R settings, by default R might create a PDF graphical devices (for more information about graphical devices in the context of rpy2, see the doc: https://rpy2.github.io/doc/v3.1.x/html/graphics.html#graphical-devices ). 根据您的本地R设置,默认情况下,R可能会创建PDF图形设备(有关rpy2上下文中图形设备的更多信息,请参见doc: https ://rpy2.github.io/doc/v3.1.x /html/graphics.html#graphical-devices )。

Regarding Python terminating, are you using Python interactively or you are running a script (eg, python myscript.py ) ? 关于Python终止,您是交互式使用Python还是正在运行脚本(例如python myscript.py )? If the latter, it would be normal for Python to exit after the script reached the end of its execution. 如果是后者,则在脚本到达执行结束后退出Python是正常的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM