为什么 Python 没有响应 "windows can try to restore the program. If you restore or close the program, you might lose information."

Why Python is not responding "windows can try to restore the program. If you restore or close the program, you might lose information."

我在 Jupyter notebook 中使用 python 3x,我想做的是在 jupyter notebook 中的 shell 中绘制一些 R 图。但是,问题是当我这样做时,绘图是在另一个 window 上绘制的,而不是在 shell 上绘制的。但是,当我关闭它时,jupyter notebook 给我一个错误 "Dead Kernel"

我的代码是:

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

关闭程序后我得到这个

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

谢谢

我要感谢我的好朋友 Sarunas 的疯狂想法。特别是,他告诉我有一条出路可以通过 "instead of showing the picture (in the window) you could try to write it as png or another image format? Then open it with some photo viewer? "

这正是我所做的! 考虑例如,我想显示一个来自 R 的数字,比如从 x=-pi 到 2pi

的 sin(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")

输出是

这是对上述问题的另一个回答,正是使用上述问题中提出的论点。而是味道

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