我正在尝试使用 rpy2 库在 python 3 中绘制一个简单的图形:
import rpy2.robjects as robjects
r = robjects.r
x = robjects.IntVector(range(10))
y = r.rnorm(10)
r.X11(1,1)
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"
但我得到这个错误:
/Users/Irbin/anaconda3/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:145: RRuntimeWarning: Error in (function (display = "", width, height, pointsize, gamma, bg, :
X11 is not available
warnings.warn(x, RRuntimeWarning)---------------------------------------------------------------------------
RRuntimeError Traceback (most recent call last)
<ipython-input-3-bdd0b9cb9184> in <module>()
----> 1 import codecs, os, ast;__pyfile = codecs.open('''/var/folders/7q/2v_j6pcx0bb9jq2r_4b2b30w0000gn/T/pyDtdUIr''', encoding='''utf-8''');__code = __pyfile.read().encode('''utf-8''');__pyfile.close();os.remove('''/var/folders/7q/2v_j6pcx0bb9jq2r_4b2b30w0000gn/T/pyDtdUIr''');__block = ast.parse(__code, '''/Users/Irbin/Downloads/scratch.py''', mode='exec');__last = __block.body[-1];__isexpr = isinstance(__last,ast.Expr);__block.body.pop() if __isexpr else None;exec(compile(__block, '''/Users/Irbin/Downloads/scratch.py''', mode='exec'));eval(compile(ast.Expression(__last.value), '''/Users/Irbin/Downloads/scratch.py''', mode='eval')) if __isexpr else None
~/Downloads/scratch.py in <module>()
6 # r Into (run inside sub codes)
7
----> 8
9 import pdb
10
~/anaconda3/lib/python3.6/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs)
176 v = kwargs.pop(k)
177 kwargs[r_k] = v
--> 178 return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
179
180 pattern_link = re.compile(r'\\link\{(.+?)\}')
~/anaconda3/lib/python3.6/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs)
104 for k, v in kwargs.items():
105 new_kwargs[k] = conversion.py2ri(v)
--> 106 res = super(Function, self).__call__(*new_args, **new_kwargs)
107 res = conversion.ri2ro(res)
108 return res
RRuntimeError: Error in (function (display = "", width, height, pointsize, gamma, bg, :
X11 is not available
我用谷歌搜索了它,我了解到 X11 在 OSX 上对于图形来说是必需的。当我用 rpy2 检查它时,我得到以下信息:
print(robjects.r("capabilities()"))
jpeg png tiff tcltk X11 aqua
TRUE TRUE TRUE TRUE FALSE TRUE
http/ftp sockets libxml fifo cledit iconv
TRUE TRUE TRUE TRUE TRUE TRUE
NLS profmem cairo ICU long.double libcurl
TRUE TRUE TRUE TRUE TRUE TRUE
这表明 X11() 不可用(FALSE),但如果我运行本机 R 并编写相同的指令 X11() 可用,如您所见:
capabilities()
jpeg png tiff tcltk X11 aqua
TRUE TRUE TRUE TRUE TRUE TRUE
http/ftp sockets libxml fifo cledit iconv
TRUE TRUE TRUE TRUE FALSE TRUE
NLS profmem cairo ICU long.double libcurl
TRUE TRUE TRUE TRUE TRUE TRUE
那么,如何在 python 环境中获得可用于 rpy2 的 x11() ?
谢谢,