Rmarkdown 是否允许编织 matplotlib 图?如果是这样,你能帮我解决问题吗?

Does Rmarkdown allow knitting of matplotlib plots? If so, will you help me troubleshoot?

编辑 找到解决方案。

我一直在制作一个 Rmarkdown 笔记本来记录我的 python 学习。在 RStudio 中,我已经能够毫无问题地将包含我的 python 代码的文档编制成 HTML,直到我开始使用 matplotlib 绘制数据。奇怪的是,这些图是在代码块中正确生成的。但是编织后,每次都在80%的时候吐错。

这是我的示例代码:

---
title: "Python Plot"
output: html_document
---

```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = TRUE)
library(reticulate) #Allows for Python to be run in chunks
```


```{python, eval=F}
import numpy as np
trees = np.array(r.trees) #Imported an internal R dataset. It got rid of headers and first row. Don't know how to deal with that right now.
type(trees)
np.shape(trees)
print(trees[1:6,:])

import matplotlib.pyplot as plt
plt.plot(trees[:,0], trees[:,1])
plt.show()
plt.clf() #Reset plot surface
```

再一次,这个情节在块内处理时表现得很好,但没有编织。错误消息说,

"This application failed to start because it could not find or load the Qt platform plugin "windows" in ",

Reinstalling the application may fix this problem."

我已经卸载并重新安装了 Rstudio 和 Python 并继续得到相同的结果。我觉得很奇怪,它在块内工作但不编织到 HTML。我的所有其他 python 代码编织得很好。

我有

python 3.7.3(默认,2019 年 3 月 27 日,17:13:21)[MSC v.1915 64 位 (AMD64)]

Rstudio 版本 1.2.1335.

我已经阅读了其他解决方案。我相信 libEGL.dll 与所有其他 QT5*.dll 位于同一位置。

在您的导入库中,您必须安装 PyQT5 并将其导入到您的 Python 环境中。因此,例如,我的第一个块如下所示,# Base Libraries 的第一行是 import PyQt5:

---
title: "Cancellations TS"
author: "Bryan Butler"
date: "7/1/2019"
output:
    html_document:
    toc: false
    toc_depth: 1
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, cache.lazy = FALSE)
```

# <strong>Time Series of Auto Policies</strong> {.tabset .tabset-fade .tabset-pills}


<style>
  .main-container {
    max-width: 1200px !important;
    margin-left: auto;
    margin-right: auto;
  }
</style>

{r, loadPython}
library(reticulate)
use_condaenv('timeseries')


## Load Python
{python importLibaries}

# Base libraries
import PyQt5
import pandas as pd
from pandas import Series, DataFrame
from pandas.plotting import lag_plot


import numpy as np
import pyodbc

我能够 运行 您的代码并进行一些修改。我将块分解以进行错误检查。你需要import numpy as np,我加了其他的。这是我开始工作的代码。此外,我使用 conda 虚拟环境,因此 Python 环境是准确的。这是有效的:

---
title: "test"
author: "Bryan Butler"
date: "7/2/2019"
output: html_document
---

```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = TRUE)
library(reticulate) #Allows for Python to be run in chunks
use_condaenv('timeseries')
```

```{python import}
import PyQt5
import pandas as pd
from pandas import Series, DataFrame
import numpy as np
import matplotlib.pyplot as plt
```

```{python, test}
trees = np.array(r.trees) #Imported an internal R dataset. It got rid of headers and first row. Don't know how to deal with that right now.
type(trees)
np.shape(trees)
print(trees[1:6,:])
```

```{python plot}
plt.plot(trees[:,0], trees[:,1])
plt.show()
plt.clf() #Reset plot surface
```

我在 Kevin Arseneau 那里找到了答案。

我不认为这是重复的,因为问题不同,但解决方案适用于这两个问题。

需要的是添加如下代码:

import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = '/path/to/Anaconda3/Library/plugins/platforms'

这是我更新后的工作代码。它类似于原始问题,并更新了 Bryan 建议的 python 导入块。

---
title: "Python Plot"
output: html_document
---

```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = TRUE)
library(reticulate) #Allows for Python to be run in chunks
```

```{python import}
import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = '/path/to/Anaconda3/Library/plugins/platforms'
import numpy as np
import matplotlib.pyplot as plt


```{python, eval=TRUE}

trees = np.array(r.trees) #Imported an internal R dataset. It got rid of headers and first row. Don't know how to deal with that right now.
type(trees)
np.shape(trees)
print(trees[1:6,:])


plt.plot(trees[:,0], trees[:,1])
plt.show()
plt.clf() #Reset plot surface
```