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?
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With