Reputation: 31
I am trying to display a plot in Pycharm Comunity Edition and I get the following error:
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so >cannot show the figure.
I have installed and imported tkinter
before importing matplotlib
but I cannot make Matplotlib switch to 'TKAgg'.
I tried running
matplotlib.use('TKAgg', force=True)
and
plt.switch_backend('tkagg')
and they both result in the following error:
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive >framework, as 'qt' is currently running
I also tried adding MPLBACKEND=TkAgg to the environment variables but did not change anything.
Does anyone have an idea what else I can do to solve this so that I can display graphs?
Upvotes: 2
Views: 4640
Reputation: 1
After upgrading to 24.04 I ran into a problem resulting in this error: ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'gtk3' is currently running.
After testing with various ways of import of mathplotlib I ended up with this which work for my program:
import matplotlib.pyplot as plt
import matplotlib as mpl
print ("matplot-versjon: ", mpl.__version__)
tekst = mpl.get_backend()
print("Backend = ", tekst)
from matplotlib import image as mpimg
plt.switch_backend('TkAgg') # This line seems to be the one that made the difference.
(I seem to have no control over the formatting of this text. Everything is there, but linefeeds are missing here and there.
Upvotes: 0
Reputation: 1
I had the same issue, and I somehow got it to work by putting the
matplotlib.use("TkAgg")
statement at the end of my module imports.
Essentially, the call to TkAgg was the last thing prior to the code. Not sure how it worked, but it worked for me. I think another module I was originally importing after the TkAgg statement was the issue.
I have tried putting the TkAgg statement back early in the module imports, and the issue comes back. When it is the last thing listed before my code starts, it works fine.
Upvotes: 0