gpt4 book ai didi

python - 使用 reticulate 在 Rstudio 中导入 matplotlib.pyplot

转载 作者:行者123 更新时间:2023-12-01 09:31:18 29 4
gpt4 key购买 nike

我无法import matplotlib.pyplot as plt在 Rstudio 中。

我尝试了 SO 上已有的解决方案。

这是我的尝试:

我通过 devtools 安装方式安装了网状包。 devtools::install_github(rstudio/reticulate)

library(reticulate)

repl_python()

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0,10, 100)
plt.plot(x, x, label = "linear")
plt.legend()
plt.show()

我收到以下错误 - RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa ImportError: numpy.core.multiarray failed to import

我也在 RMarkdown 中尝试过。

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(reticulate)
use_condaenv(condaenv = "python36", conda = "auto", required = FALSE)
```

```{python}
repl_python()
import numpy as np
import matplotlib.pyplot as plt

n = 256
X = np.linspace(-np.pi,np.pi,n,endpoint=True)
Y = np.sin(2*X)

plt.plot (X, Y+1, color='blue', alpha=1.00)
plt.plot (X, Y-1, color='blue', alpha=1.00)
plt.show()
```

收到以下错误 -

RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa
Traceback (most recent call last):
File "C:\Users\Vidhya\AppData\Local\Temp\Rtmp6vCzV6\chunk-code-13e83c491e61.txt", line 2, in <module>
import matplotlib.pyplot as plt
File "C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 29, in <module>
import matplotlib.colorbar
File "C:\Anaconda3\lib\site-packages\matplotlib\colorbar.py", line 32, in <module>
import matplotlib.artist as martist
File "C:\Anaconda3\lib\site-packages\matplotlib\artist.py", line 15, in <module>
from .transforms import (Bbox, IdentityTransform, TransformedBbox,
File "C:\Anaconda3\lib\site-packages\matplotlib\transforms.py", line 39, in <module>
from matplotlib._path import (affine_transform, count_bboxes_overlapping_bbox,
ImportError: numpy.core.multiarray failed to import

最佳答案

我也遇到过类似的问题。我做了以下操作并解决了。在此之前,我想在我获得解决方案的以下线程中向您推荐...

Qt platform plugin issue Rstudio

PyQt5 - Failed to load platform plugin "windows". Available platforms are: windows, minimal

这是您必须做的[根据您的系统更改路径]

在 R 中:-

py_run_string("import os as os")
py_run_string("os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = 'C:/Dev/Anaconda/Library/plugins/platforms'")
np <- import("numpy", as = "np")
plt <- import("matplotlib.pyplot", as = "plt")

x <- np$linspace(0,10, 100)
plt$plot(x, x, label = "linear")
plt$legend()
plt$show()

在repl_python(嵌入模式)中

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0,10, 100)
plt.plot(x, x, label = "linear")
plt.legend()
plt.show()

希望这有帮助......干杯

关于python - 使用 reticulate 在 Rstudio 中导入 matplotlib.pyplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49953620/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com