简体   繁体   English

在R中使用网状图导入matplotlib

[英]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. 我刚开始在R中使用网状包,但仍然发现了一些问题。 In particular, importing matplotlib is not going well. 特别是,导入matplotlib进行得并不顺利。 I've tried it two different ways, with different error messages for each. 我尝试了两种不同的方法,每种方法都有不同的错误消息。

First, using repl_python in RStudio's interactive shell: 首先,在RStudio的交互式shell中使用repl_python:

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: 打开的REPL Python外壳似乎具有正确的版本和所有内容,但是当我尝试导入matplotlib.pyplot时,看到以下内容:

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) ImportError:/lib/x86_64-linux-gnu/libz.so.1:未找到版本“ ZLIB_1.2.9”(/ 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. 安装zlib(使用sudo apt-get install lib64z1-dev lib64z1 )似乎没有任何改变。 FWIW, import matplotlib worked just fine, as long as I don't need pyplot . FWIW,只要我不需要pyplotimport matplotlib就可以了。

I also tried doing the same thing in an R Markdown document: 我还尝试在R Markdown文档中做同样的事情:

```{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 py_get_attr_impl(x,名称,无提示)中的错误:AtributeError:模块'matplotlib'没有属性'pyplot'调用:... $ .python.builtin.object-> py_get_attr-> py_get_attr_impl-> .Call执行暂停

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: 如果我尝试运行此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. 当我尝试搜索错误文本时,据我所知,似乎与xcb相似的错误确实出现了,但并不是很相关。

I was able to get things working by changing the R Markdown code block to read: 通过将R Markdown代码块更改为以下内容,我可以使工作正常进行:

```{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. 我仍然不太明白为什么,但是似乎reticulate在anaconda的安装中效果不佳。 Maybe it has something to do with anaconda being set up to work well with an interactive Jupyter notebook. 也许与anaconda有关,该anaconda被设置为可以与交互式Jupyter笔记本一起正常工作。

I was able to get it to work with my conda install by sym linking the conda lib file to /lib/x86_64-linux-gnu/. 我可以通过将conda lib文件链接到/ lib / x86_64-linux-gnu /将它与symda安装一起使用。

ln -s -f /opt/miniconda/lib/libz.so.1 /lib/x86_64-linux-gnu/libz.so.1

I noticed that if I ran python alone with the same import it worked fine. 我注意到,如果我使用相同的导入单独运行python,则效果很好。 It appears that reticulate isn't 'seeing' the conda lib as a source for libz but does look in the /lib/x86_64-linux-gnu/ directory. 网状网似乎没有“将conda lib视为” libz的源,而是在/lib/x86_64-linux-gnu/目录中查找。

Python: 3.6 的Python:3.6
Conda: 4.5.1 康达:4.5.1
OS: Ubuntu 14.04.1 LTS 作业系统:Ubuntu 14.04.1 LTS

I found the same error with reticulate, which is not reading the zlib from the anaconda library but from /lib/x86_64-linux-gnu/. 我发现网状结构存在相同的错误,它不是从anaconda库中读取zlib,而是从/ lib / x86_64-linux-gnu /中读取。

Instead of symlinking, I just run the following line from terminal each time I'm using the script: 每次使用脚本时,我都从终端运行以下行,而不是符号链接:

export LD_LIBRARY_PATH=/home/craig/anaconda3/lib/:$LD_LIBRARY_PATH

You can actually run it from inside the R script, giving: 您实际上可以从R脚本内部运行它,得到:

system('export LD_LIBRARY_PATH=/home/craig/anaconda3/lib/:$LD_LIBRARY_PATH')

I have been working with reticulate and R Markdown and you should specify your virtual environment. 我一直在使用网状结构和R Markdown,您应该指定您的虚拟环境。 For example my R Markdown starts as follows: 例如,我的R Markdown开始如下:

{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, cache.lazy = FALSE)
library(reticulate)

use_condaenv('pytorch')

Then you can work in either language. 然后,您可以使用任何一种语言。 So, for plotting with matplotlib, I have found that you need the PyQt5 module to make it all run smoothly. 因此,对于使用matplotlib进行绘图,我发现您需要使用PyQt5模块才能使其顺利运行。 The following makes a nice plot inside R Markdown. 以下是R Markdown内部的一个不错的图。

{python plot}
import PyQt5
import numpy as np
import pandas as pd
import os

import matplotlib.pyplot as plt
from matplotlib.pyplot import figure

data = pd.read_csv('Subscriptions.csv',index_col='Date', parse_dates=True)

# make the nice plot
# set the figure size
fig = plt.figure(figsize = (15,10))

# the series
ax1 = fig.add_subplot(211)
ax1.plot(data.index.values, data.Opens, color = 'green', label = 'Opens')

# plot the legend for the first plot
ax1.legend(loc = 'upper right', fontsize = 14)

plt.ylabel('Opens', fontsize=16)

# Hide the top x axis
ax1.axes.get_xaxis().set_visible(False)

#######  NOW PLOT THE OTHER SERIES ON A SINGLE PLOT

# plot 212 is the MI series

# plot series
ax2 = fig.add_subplot(212)
ax2.plot(data.index.values, data.Joiners, color = 'orange', label = 'Joiners')

# plot the legend for the second plot
ax2.legend(loc = 'upper right', fontsize = 14)

# set the fontsize for the bottom plot
plt.ylabel('Joiners', fontsize=16)

plt.tight_layout()
plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM