reticulate + RStudio + matplotlib = ❤ ?

I've seen things on the internet such as
https://rstudio.github.io/reticulate/articles/rstudio_ide.html
hinting towards the fact that I should be able to get matplotlib plots output on the RStudio console, but I haven't been able to get this to work. Does anyone know how to do it?

This is a little .Rmd example including matplotlib output.

---
title: "Matplotlib output"
output: html_notebook
---

```{r}
library(reticulate)
use_python("/usr/bin/python3") # Change accordingly to your Python version
matplotlib <- import("matplotlib")
matplotlib$use("Agg", force = TRUE)
```

```{python}
import pandas as pd
import matplotlib.pyplot as plt
df=pd.DataFrame([[1, 2], [3, 4], [4, 3], [2, 3]])
fig = plt.figure(figsize=(14,8))
for i in df.columns:
    ax=plt.subplot(2,1,i+1) 
    df[[i]].plot(ax=ax)
    print(i)

plt.show()
```

1 Like

I seem to have gotten R markdown to work, but what about on the console?
Thanks for looking into this! :slight_smile:

As far as I know, console output (to the viewer pane) for matplotlib is not supported by RStudio but I might be wrong.

The page I linked says " * Display of matplotlib plots within both notebook and console execution modes."

You can import python modules this way but I don't know how to use matplotlib with this syntax :man_shrugging:

library(reticulate)
use_python("/usr/bin/python3")
matplotlib <- import("matplotlib")
matplotlib$use("Agg", force = TRUE)
plt <- import("matplotlib.pyplot")
fig <- plt$figure(figsize=c(14,8))
> library(reticulate)
> matplotlib <- import("matplotlib")
> matplotlib$use("Agg", force = TRUE)
> plt <- import("matplotlib.pyplot")
> fig <- plt$figure(figsize=c(14,8))
> plt$plot(1:10,1:10)
[[1]]
Line2D(_line0)

but nothing gets displayed.

You will need to call plt.show() to show the plot, and I believe that will only work when using repl_python() explicitly. Automatic plot showing is only implemented in the R Markdown executing mode.

(also worth saying -- you'll need to make sure you have the R png package installed, although that should automatically be installed for you when a Python file is open)

2 Likes

I was very tired, that sounds consistent with my experience. Is there any hope for, or a workaround for automatic plot showing in the console?

Was my quote from the release page misleading or did I misunderstand/misread something?

This is what I get when I attempt via repl_python():

> library(reticulate)
> repl_python()
Python 3.7.3 (/nix/store/dp7m97wmfraarrnp5hxsqz1czm06v1rr-python3-3.7.3-env/bin/python)
Reticulate 1.12.0.9001 REPL -- A Python interpreter in R.
>>> import matplotlib
>>> matplotlib.use("Agg", force=True)
>>> import matplotlib.pyplot as plt
>>> plt.figure(figsize=(14,8))
<Figure size 1400x800 with 0 Axes>
>>> plt.plot([1,2,3],[1,2,3])
[<matplotlib.lines.Line2D object at 0x7faa70cf2160>]
>>> plt.show()
/nix/store/dp7m97wmfraarrnp5hxsqz1czm06v1rr-python3-3.7.3-env/lib/python3.7/site-packages/matplotlib/figure.py:445: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
  % get_backend())

Thanks for your time!

PS: It would be handy to get these plots without blocking the R repl. I tried some horrible hacks with forking and whatnot, but nothing quite worked fully.

We intended to highlight that plots should work in two contexts:

  1. In R Markdown documents (R Notebooks), with auto-printing as one might see within e.g. Jupyter Notebooks;
  2. When the Python REPL is active, as through repl_python().

I think perhaps we were too succinct in our description here but otherwise things should work as documented. The main missing piece is getting things like plt$show() to work when invoked from R code directly, I believe.

Would you mind filing the missing bits here as a feature request at Issues · rstudio/rstudio · GitHub, so we don't lose track of it?

I may not have been clear, my previous code snippet showed the repl_python() show() call failing. Thats supposed to work right?

That should indeed work. Can you confirm which version of RStudio you're using (+ what OS)?

RStudio 1.2.1335 and I'm on Linux.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.