Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: use() got an unexpected keyword argument 'warn' when importing matplotlib

I am attempting to learn R markdown / notebook to implement chunks of code which are R and Python. I can use R to a reasonable standard, but I am new to Python. I have downloaded R markdown to R studio and followed the prompts to download miniconda and did so.

I wanted to make a basic plot to see if it is working.

I click 'insert a new Python chunk'

Then copy and paste the following code into a 'Python chunk', which worked fine in Python using the Spyder environment.

import matplotlib.pyplot as plt 
x = [2, 4, 6, 8, 10, 12]
y = [1, 3, 5, 6, 9, 15] 
plt.plot(x, y)
plt.show()

The following error is returned:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: use() got an unexpected keyword argument 'warn'

I had installed matplotlib using the following in an r chunk:

library(reticulate)
py_install("matplotlib")

I have tried pasting this chunk into a Python chunk and an R chunk with:

{r,engine='python'}

But to no avail. I can't see anyone having the same error or a guide specifying how to be able to import from Python, it all seems to show these chunks working straight out of the box. Apologies if I am missing something glaringly obvious. I have made sure all other versions of Python are uninstalled. Any advice would be greatly appreciated, as I one day hope to be able to integrate Python, R and bash scripts in this thing!

Session info:

Windows 10, R version 3.6.3 (2020-02-29), R Studio Version 1.2.5001. R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252 
[2] LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] knitr_1.28      reticulate_1.16

loaded via a namespace (and not attached):
[1] compiler_3.6.3  Matrix_1.2-18   tools_3.6.3     rappdirs_0.3.1 
[5] Rcpp_1.0.4.6    grid_3.6.3      jsonlite_1.6.1  xfun_0.9       
[9] lattice_0.20-38
like image 853
Benjamin Simpson Avatar asked Jul 22 '20 14:07

Benjamin Simpson


1 Answers

I had the exact same error when using reticulate and matplotlib.

I fixed it by downgrading my matplotlib version, e.g.:

pip install matplotlib==3.2  

You can verify that matplotlib has been downgraded by running pip freeze and finding the matplotlib entry. It should look like matplotlib==3.2.0. (maybe try doing this from r, so you can be sure that you're working with the correct python installation).

I think this issue is caused by the 'warn' parameter being removed from a matplotlib function in recent versions of matplotlib, and R/reticulate assumes the older behaviour of the function.

like image 143
honk Avatar answered Oct 29 '22 08:10

honk