2017-09-22 22 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

相關問題