Home > Software design >  How to use matplotlib backend "TkAgg" in Jupyter
How to use matplotlib backend "TkAgg" in Jupyter

Time:04-22

When running the following code in the Jupyter on Ubuntu 20.04:

import matplotlib
try:
    matplotlib.use("TkAgg")
    import matplotlib.pyplot as plt
except ImportError as e:
    print(e)
    plt = None

assert plt is not None, "matplotlib backend failed"
print("done")

Here is the output:

Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
/tmp/ipykernel_5612/97292621.py in <cell line: 9>()
      7     plt = None
      8 
----> 9 assert plt is not None, "matplotlib backend failed"
     10 print("done")

AssertionError: matplotlib backend failed

But I can run it directy from python3 command line without any problem.

Is there any python or system package need to be import or install first for running it in Jupyter notebook?

CodePudding user response:

Try running this command on a empty cell:

%matplotlib tk

Then, execute your plotting commands.

  • Related