pythonrmatplotlibr-markdownqtplugin

R Markdown: could not find or load Qt platform plugin - matplotlib


While doing some Monte Carlo simulations I've encountered a problem with creating plots with matplotlib in python chunks of code in R Markdown. I get

enter image description here

while using even the simplest test file with matplotlib and a hisogram of a list:

---
title: "Test"
author: "Me"
date: "19 04 2021"
output:
pdf_document: default
word_document: default
```

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(reticulate)
use_python("C:/Users/PC/anaconda3/python.exe")
```

```{python}
import matplotlib.pyplot as plt
T=[1,7,3,6,2,8,3,7,2,9,4,3,7,2,8,5,4,3]
plt.hist(T)
plt.show()
```

I have reinstalled MiKTeX and RStudio, switched to Anaconda, added some varibles to both PATH and as new, for example QT_PLUGIN_PATH.

I DO NOT have this problem while using PyCharm with either this or a more complicated code. Please help me! Any and all tips appreciated. Let me know, what other info should I provide to find the origin of the problem and failed 'solutions'.


Solution

  • I've reinstalled both RStudio and python itself, adding it to PATH during the installation. I turned back from using anything provided by Anaconda. I've reduced the code in given example to:

    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    library(knitr)
    library(reticulate)
    ```
    
    ```{python}
    import matplotlib.pyplot as plt
    T=[1,7,3,6,2,8,3,7,2,9,4,3,7,2,8,5,4,3]
    plt.hist(T)
    plt.s
    ```
    

    AND installed matplotlib alongside other packages via Windows PowerShell:

    pip3 install matplotlib
    

    as I found advised in here: https://psu-psychology.github.io/psy-525-reproducible-research-2020/how_to/python-in-rmarkdown.html

    After that I knitted the R Markdown file without any problems.