Community Conundrum 25:Feature Visualization is now live! Read More

How to import the seaborn python library (based on matplotlib) in a RMarkdown report?

Dataiker
Dataiker
How to import the seaborn python library (based on matplotlib) in a RMarkdown report?

Hello,



In a python notebook, I have succeeded in importing and using the seaborn library (based on matplotlib).



However, in a Rmarkdown report, I have the following error ImportError: No module named Tkinter.



How to use seaborn in a Rmarkdown report?



Sam

0 Kudos
1 Reply
Dataiker
Dataiker
Author

Hello Sam,



The seaborn python library is indeed using matplotlib. This library contains multiple backends that are not necessarily installed or configured. When loading seaborn, it will try to load the default backend based on TCL/TK (UI framework) which is bundled in the Tkinter python package.



You need to use matplotlib.use("Agg") (or any other properly configured backend) before importing seaborn in order to force matplotlib to use the correct backend. For information, the Agg backend only computes the plots without displaying them.



In notebooks, the magic first line %pylab inline will trigger this method in order to use a backend that sends the output as HTML to the notebook.



Sam

0 Kudos
Labels (2)