Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing matplotlib with reticulate in R

I just started using the reticulate package in R, and I'm still getting a few of the kinks figured out. In particular, importing matplotlib is not going well. I've tried it two different ways, with different error messages for each.

First, using repl_python in RStudio's interactive shell:

library(reticulate)
use_python('/home/craig/anaconda3/bin/python')
py_discover_config()
repl_python()
import matplotlib.pyplot as plt

The REPL Python shell that opens up seems to have the correct version and everything, but when I try to import matplotlib.pyplot, I see the following:

ImportError: /lib/x86_64-linux-gnu/libz.so.1: version `ZLIB_1.2.9' not found (required by /home/craig/anaconda3/lib/python3.6/site-packages/matplotlib/../../.././libpng16.so.16)

Installing zlib (using sudo apt-get install lib64z1-dev lib64z1) didn't seem to change anything. FWIW, import matplotlib worked just fine, as long as I don't need pyplot.

I also tried doing the same thing in an R Markdown document:

```{r}
library(reticulate)
py_discover_config()
```

```{python}
import matplotlib.pyplot as plt
```

This time I saw:

Error in py_get_attr_impl(x, name, silent): AtributeError: module 'matplotlib' has no attribute 'pyplot' Calls: ... $.python.builtin.object -> py_get_attr -> py_get_attr_impl -> .Call Execution halted

Any ideas what might be going on here?

Thanks!

UPDATE: As I mentioned in the comments, installing the developer version of reticulate fixes some of the problems, but not all. If I try to run this Rmd:

```{r}
library(reticulate)
use_python('/home/craig/anaconda3/bin/python')
```

```{python}
import matplotlib.pyplot as plt
```

I get the following error messages:

Error in py_run_string_impl(code, local, convert) : 
  ImportError: /home/craig/anaconda3/lib/python3.6/site-packages/PyQt5/../../../libxcb-dri3.so.0: undefined symbol: xcb_send_request_with_fds

Detailed traceback: 
  File "<string>", line 1, in <module>
  File "/home/craig/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 116, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/home/craig/anaconda3/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 60, in pylab_setup
[backend_name], 0)
  File "/home/craig/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5agg.py", line 16, in <module>
    from .backend_qt5 import (
  File "/home/craig/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5.py", line 18, in <module>
    import matplotlib.backends.qt_editor.figureoptions as figureoptions
  File "/home/craig/anaconda3/lib/python3.6/site-packages/matplotlib/backends/qt_editor/figureoptions.py", line 20, in <module>
Calls: <Anonymous> ... force -> py_run_string -> py_run_string_impl -> .Call
Execution halted

When I tried googling the error text, a similar error with xcb does seem to be coming up in a context that is, as far as I can tell, not so relevant.

like image 787
cjolley Avatar asked Apr 10 '18 23:04

cjolley


People also ask

Can you use matplotlib in R?

Plotting a Graph: In last part we will plot graph by using python and matplotlib in R. now we have to copy vector in python to plot it by running command Pyrun. Code below shows how to plot sin and cosine by using plt. plot() based on matplotlib.

How do I import a Python module in R?

Importing Python modules You can use the import() function to import any Python module and call it from R. For example, this code imports the Python os module and calls the listdir() function: library(reticulate) os <- import("os") os$listdir(".")

What is reticulate R?

The reticulate package provides a comprehensive set of tools for interoperability between Python and R. The package includes facilities for: Calling Python from R in a variety of ways including R Markdown, sourcing Python scripts, importing Python modules, and using Python interactively within an R session.

What is %Matplotlib inline?

%matplotlib inline turns on “inline plotting”, where plot graphics will appear in your notebook. This has important implications for interactivity: for inline plotting, commands in cells below the cell that outputs a plot will not affect the plot.


1 Answers

I was able to get things working by changing the R Markdown code block to read:

```{r}
library(reticulate)
use_python('/usr/bin/python3')
```

```{python}
import matplotlib.pyplot as plt
```

I still don't really understand why, but it seems that reticulate doesn't play nice with anaconda installations. Maybe it has something to do with anaconda being set up to work well with an interactive Jupyter notebook.

like image 170
cjolley Avatar answered Sep 21 '22 14:09

cjolley