2017-09-22 120 views
2

我在Jupyter笔记本中使用python 3x,我想要做的是在jupyter笔记本中绘制一些R图。然而,问题是,当我这样做时,情节被绘制在另一个窗口而不是在外壳中。然而,当我关闭它的jupyter笔记本给我一个错误“死内核”为什么Python没有响应“windows可以尝试恢复程序,如果你恢复或关闭程序,你可能会失去信息。”

enter image description here

我的代码是:

# To fit a restricted VAR model we will have to call the function from R 

# Call function from R 
from rpy2.robjects.packages import importr 
from rpy2.robjects import pandas2ri 
pandas2ri.activate() 

# Calling packages 
import pandas as pd, numpy as np 

# Test for serial correlation 
MTS = importr("MTS", lib_loc = "C:/Users/Rami Chehab/Documents/R/win-library/3.3") 
RMTSmq=MTS.mq 

# Create data 
df = pd.DataFrame(np.random.random((108, 2)), columns=['Number1','Number2']) 

# Test for data 
RMTSmq(df, adj=4) 

后我关闭程序我得到这个

enter image description here

有人能帮助我吗?我希望如果可能的话,我可以在jupyter笔记本中绘制图表。

谢谢

回答

0

这是另外一个答案上述问题恰恰使用了上述问题中提出的论点。相反的味道

import rpy2.robjects as ro, pandas as pd, numpy as np 
from rpy2.robjects.packages import importr 

# Call function from R 
import rpy2.robjects as robjects 
from rpy2.robjects import r 
from rpy2.robjects.numpy2ri import numpy2ri 
from rpy2.robjects.packages import importr 

# To plot drawings in R 
grdevices = importr('grDevices') 

# Save the figure as Rami1.png 
grdevices.png(file="Rami1.png", width=512, height=512) 

# We are interested in finding if there is any serial correlation in the Multivariate residuals 
# Since there is a fitting VAR it will be cumbersome to create this function here therefore consider 
# that residauls resi as follow 
resi = pd.DataFrame(np.random.random((108, 2)), columns=['Number1','Number2']) 

# firt take the values of the dataframe to numpy 
resi1=np.array(resi, dtype=float) 

# Taking the variable from Python to R 
r_resi = numpy2ri(resi1) 

# Creating this variable in R (from python) 
r.assign("resi", r_resi) 

# Calling libraries in R for mq to function which is MTS 
r('library("MTS")') 

# Calling a function in R (from python) 
p = ro.r('result <-mq(resi,adj=4)')  
grdevices.dev_off() 

from IPython.display import Image 
Image("Rami1.png") 

enter image description here

0

我想感谢我的好朋友萨鲁纳斯他疯狂的想法。特别是,他告诉我有一条出路,“不用展示图片(在窗口中),你可以尝试将它写成PNG或其他图像格式?然后用一些照片查看器打开它?”

这正是我所做的! 例如考虑我想显示来自R的人物从x = -pi直到二皮(X)说罪的情节

import rpy2.robjects as ro 
from rpy2.robjects.packages import importr 

grdevices = importr('grDevices') 


grdevices.png(file="Rami1.png", width=512, height=512) 
p = ro.r('curve(sin, -pi, 2*pi)')  
grdevices.dev_off() 
print() 

from IPython.display import Image 
Image("Rami1.png") 

输出是 enter image description here

相关问题