简体   繁体   English

如何在 Jupyter Notebook 中使用 rpy2 绘图(内联)?

[英]How to plot (inline) with rpy2 in Jupyter notebook?

I'm learning to use rpy2 in Jupyter notebook.我正在学习在 Jupyter 笔记本中使用 rpy2。 I'm having troubles with the plotting.我在绘图方面遇到了麻烦。 When I use this example from the rpy2 docs for interactive work:当我使用 rpy2 文档中的这个示例进行交互式工作时:

from rpy2.interactive import process_revents
from rpy2.robjects.packages import importr
from rpy2.robjects.vectors import IntVector
process_revents.start()

graphics = importr("graphics")
graphics.barplot(IntVector((1,3,2,5,4)), ylab="Value")

Jupyter opens a new window with the plot. Jupyter 打开一个带有绘图的新窗口。 The window "title" reads: R Graphics: Device 2 (ACTIVE) (Not Responding).窗口“标题”显示为:R 图形:设备 2(活动)(无响应)。 My Jupyter kernel is active.我的 Jupyter 内核处于活动状态。 When I try to close the window with the plot, windows claims that python.exe is not responsing and if I force close then the jupyter kernel restarts.当我尝试关闭带有绘图的窗口时,windows 声称 python.exe 没有响应,如果我强制关闭,则 jupyter 内核会重新启动。

First: How can I make rpy2 plot inline?第一:如何使 rpy2 绘图内联? Second: If inline plotting is not possible, how to get the plot in a window without python.exe becoming unresponsive?第二:如果无法进行内联绘图,如何在没有 python.exe 无响应的情况下在窗口中获取绘图?

It seems that this is the answer to your question: https://bitbucket.org/rpy2/rpy2/issues/330/ipython-plotting-wrapper这似乎是您问题的答案: https : //bitbucket.org/rpy2/rpy2/issues/330/ipython-plotting-wrapper

with rpy2.robjects.lib.grdevices.render_to_bytesio(grdevices.png, width=1024, height=896, res=150) as img:
    graphics.barplot(IntVector((1,3,2,5,4)), ylab="Value")
IPython.display.display(IPython.display.Image(data=img.getvalue(), format='png', embed=True))

I think the cleanest solution is to simply use the %R magic function.我认为最干净的解决方案是简单地使用%R魔术功能。 It used to be part of IPython, but got moved to rpy2 , so you have to load it as an extension first:它曾经是 IPython 的一部分,但后来转移到rpy2 ,因此您必须首先将其作为扩展加载:

%load_ext rpy2.ipython
A = np.random.normal(100)
%R -i A hist(A)

plots a histogram to the Jupyter console.将直方图绘制到 Jupyter 控制台。

This is slightly more sopthisticated version of Christian's answer which wraps the plotting and inline embedding into the same context manager:这是 Christian's answer 的稍微更复杂的版本,它将绘图和内联嵌入包装到同一个上下文管理器中:

from contextlib import contextmanager
from rpy2.robjects.lib import grdevices
from IPython.display import Image, display

@contextmanager
def r_inline_plot(width=600, height=600, dpi=100):

    with grdevices.render_to_bytesio(grdevices.png, 
                                     width=width,
                                     height=height, 
                                     res=dpi) as b:

        yield

    data = b.getvalue()
    display(Image(data=data, format='png', embed=True))

Usage:用法:

with r_inline_plot(width=1024, height=896, dpi=150):
    graphics.barplot(IntVector((1,3,2,5,4)), ylab="Value")

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

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