Use reticulate to create plot using a pandas function within an R script

63 Views Asked by At

The below code creates a scatter plot in python using pandas plotting functions

import pandas as pd
data = {'col1': [1,2,3,4,5],
  'col2': [6,7,8,9,10]}
df = pd.DataFrame(data)
df.plot.scatter(x='col1', y='col2', alpha=0.3)

I am trying to recreate the plot function using reticulate. So far I have gotten to:

library(reticulate)
pd <- reticulate::import('pandas')
df <- data.frame(col1=c(1,2,3,4,5), col2=c(6,7,8,9,19))
#how do I recreate the python code for 
#df.plot.scatter(x='col1', y='col2', alpha=0.3) using reticulate?

I know I could do this easily using ggplot2 but the objective is to use the pandas function within an r script (not rmarkdown script) using reticulate.

Any suggestions?

1

There are 1 best solutions below

0
Basil On

You need to use r_to_py function to get the dataframe in the python environment and from there you access it using python code with the r. prefix.

library(reticulate)
plt <- reticulate::import('matplotlib.pyplot')
df <- data.frame(col1=c(1,2,3,4,5), col2=c(6,7,8,9,10))

df_python <- r_to_py(df)

py_run_string(
"
python_plot = r.df_python.plot.scatter(x='col1', y='col2', alpha=0.3)
")

plt$show(py$python_plot)