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

spicquenot
spicquenot Dataiker Posts: 4 Dataiker

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

Tagged:

Best Answer

  • spicquenot
    spicquenot Dataiker Posts: 4 Dataiker
    Answer ✓

    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

Setup Info
    Tags
      Help me…