25

When I use google colaboratory, I face this problem, I have searched it on stackoverflow, but few answer, could any guy help me to figure it out? THX!

I have tried to reinstall matplotlib in several ways, and install tk-dev, all of them don't work.

import matplotlib
import glob


**matplotlib.use('TKAgg')**


import matplotlib.image as mpimg

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.ticker as ticker

There comes out an ImportError like the Title :"ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running"

Nicolas Gervais
  • 28,901
  • 11
  • 96
  • 121
马启森
  • 251
  • 1
  • 3
  • 4

6 Answers6

12

What solved the problem for me was to restart my kernel, import the following first:

import matplotlib
matplotlib.use('TKAgg')

Then,

import matplotlib.pyplot as plt

You probably imported matplotlib with another framework before you tried to change to TKAgg. Restart your kernel.

Nicolas Gervais
  • 28,901
  • 11
  • 96
  • 121
5

matplotlib.use('TKAgg') gets there too late maybe (?). I had success setting the environment variable instead.

export MPLBACKEND=TKAgg

Really odd error message. I was also trying this inside a docker container and had to set DISPLAY as well. Maybe there are other errors that aren't being reported. Using reload(matplotlib) did not help either.

jozxyqk
  • 15,424
  • 12
  • 80
  • 168
  • That made my day! I had a BASH script running a python script and adding this before running the actual py script made it. – kimy82 May 26 '21 at 10:42
  • Tried this approach w/o success :( any more ideas? – Ilan Aizelman WS Dec 07 '21 at 12:22
  • @joyzxyqk What did you set DISPLAY to? :0.0? – snark May 04 '22 at 15:10
  • 1
    @snark For docker I assume I had `export DISPLAY=:0`, same as the host's `$DISPLAY`. I was also mounting `/tmp/.X11-unix` so that would work. I guess an alternative would be X forwarding over an ssh connection to the container. – jozxyqk May 04 '22 at 21:50
1

I solved the problem by changing matplotlib.use('TkAgg') into matplotlib.use('Agg').

0

I repeat matplotlib.use('TkAgg') again and again to fix the problem. It's weird but work for me!

import matplotlib
i = 0
while i < 10:
    i += 1
    try:
        matplotlib.use('TkAgg')
        break
    except:
        print(i)
0

For me, I was running ubuntu with docker on windows and the reason for this error was that I didn't start VcXsrv Windows X Server. when I started X Server, the error disappeared.

Adryen
  • 1
-2

The error message clearly explains that TkAgg needs tkinter library.

You can fix this error by importing tkinter library ahead of matplotlib, for example:

import tkinter
import matplotlib
matplotlib.use('TkAgg')
  • 3
    It is not required to `import tkinter` explicitly – gies0r Jul 03 '19 at 07:50
  • I agree with @gies0r , it neither fix the problem nor required to be added explicitly. – r_e Mar 12 '20 at 13:27
  • For me, this suggestion worked. (Setup: ssh -X (x forwarding) to another machine, on which I again forward x into a rootless podman container -- where I finally start python). – FZeiser May 12 '22 at 07:42