3

I'm trying to run a basic matplotlib example from the official website:

However, when i run the code, my Python interpreter complains and outputs the following message:

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
plt.show()

I've installed matplotlib via pip3 install matplotlib. My current python3 version is 3.9.1 and my OS is Ubuntu 20.04.

I've already tried installing tkinter, as already described here, with no success. What should I do? Why is this happening?

hao123
  • 381
  • 2
  • 6
  • 21

1 Answers1

6

Please try these, if any works for you:

  1. if you are using Jupyter Notebook

     %matplotlib inline 
    
  2. make sure you have tkinter, recompile python interpreter after installing tkinter

  3. try:

     import matplotlib
     import matplotlib.pyplot as plt
     plt.style.use('ggplot')
     matplotlib.use( 'tkagg' )
     x = [1, 5, 1.5, 4]
     y = [9, 1.8, 8, 11]
     plt.scatter(x,y)
     plt.show()
    
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
  • 1
    how can i recompile python interpreter? I'm not using jupyter notebook even after adding these suggested lines I'm still getting `import _tkinter # If this fails your Python may not be configured for Tk ModuleNotFoundError: No module named '_tkinter'` – hao123 Mar 12 '21 at 23:47
  • why are you using an underscore before tkinter? – Talha Tayyab Mar 13 '21 at 01:51
  • 1
    I'm not This is the error that it gives. I'm running the `matplotlib` example exactly the same as described in the website – hao123 Mar 13 '21 at 13:33