Reputation: 717
I am trying to do a seaborn heatmap plot via RStudio.
I usereticulate
package in R.
Below is my code:
library(reticulate)
use_condaenv("python36", conda = "auto", required = FALSE)
os <- import("os")
os$listdir(".")
py_available()
sns <- import('seaborn')
plt <- import('matplotlib.pyplot')
pd <- import('pandas')
dat <- AirPassengers
# convert time series to data frame
dat <- data.frame(matrix(dat, ncol=frequency(dat), dimnames=dimnames(.preformat.ts(dat)) ))
dat
sns$heatmap(r_to_py(dat), fmt = "g", cmap = "viridis")
plt$show()
However, I receive the following error and my R session gets aborted when it reaches the seaborn heatmap line. What should I do to fix this error?
Upvotes: 4
Views: 2730
Reputation: 11
Here is a script to run directly in R, for Anaconda users:
library(reticulate)
use_condaenv(condaenv = "your conda env", required = T)
py_run_string('import os')
py_run_string("os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = '/path/to/Anaconda3/Library/plugins/platforms'")
np = import("numpy",delay_load = T)
plt = import("matplotlib.pyplot",delay_load = T)
t = np$arange(0.01, 10.0, 0.01)
data1 = np$exp(t)
data2 = np$sin(2 * np$pi * t)
#These last lines must be run together:
fig = plt$figure(figsize=c(14,8))
plt$plot(1:10,1:10)
plt$show()
Enjoy!!!
Upvotes: 1
Reputation: 46
I'm not using Anaconda but using miniconda and reticulate for R-studio
So I used
import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = 'C:\Users\<user-name>\AppData\Local\r-miniconda\envs\r-reticulate\Library\plugins\platforms'`
It worked, Thank you @Shepherd and @F0nzie
Upvotes: 0
Reputation: 1205
I had the same problem with RStudio daily build 1.2.114 and a Anaconda Python 3.7 environment where I had PyTorch and matplotlib
installed.
I followed the instructions by @Sheperd, with the following changes, pointing to the environment where you have matplotlib
installed; in my case pytorch37
:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = 'C:/Users/user_name/Anaconda3/envs/pytorch37/Library/plugins/platforms'
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks')
ax.grid()
plt.show()
Now, PyQt
is found and RStudio doesn't crash anymore.
Upvotes: 5
Reputation: 438
this seems like a duplicate question. I am using RStudio-1.2.679 with R-3.4.4 to write and edit Python code. I was having the exact same problem, I tried many solutions but nothing seemed to work. Finally I found the solution here -I take absolutely no credit for it. This is, at the top of your Python code (file with extension .py) where you import libraries include:
import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = 'C:/Users/myusername/AppData/Local/Continuum/Anaconda3/Library/plugins/platforms'
Notice that is how the path looks like in my PC, it may look different in yours.
Following this example:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = 'C:/Users/myusername/AppData/Local/Continuum/Anaconda3/Library/plugins/platforms'
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks')
ax.grid()
plt.show()
This shows the plot in the "Plots" panel in RStudio. Best wishes!
Upvotes: 4