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

293 Views Asked by At

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'.

2

There are 2 best solutions below

0
On BEST ANSWER

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.

2
On

I found the solution on the qt forums. Visit this site for more reference https://www.dropboxforum.com/t5/Dropbox-installs-integrations/How-do-I-fix-the-Qt-platform-plugin-quot-windows-quot-error/td-p/91545.

My problem originated with setting the environment variables to get anaconda working.

QT_PLUGIN_PATH=c:\Anaconda3\Library\plugins\

But by setting this it breaks everything else. So for now I've just gotten rid of the path variable.

To check if you have the env variable set

  1. Press + R
  2. Type "Control Panel"
  3. Open system
  4. Open advanced system settings on the left
  5. Click on the environment variables button at the bottom
  6. Browse through the user and system lists to see if QT_PLUGIN_PATH has been set.