I am trying to plot a simple graph using pyplot, e.g.:
import matplotlib.pyplot as plt
plt.plot([1,2,3],[5,7,4])
plt.show()
but the figure
Just in case if this helps anybody.
Python version: 3.7.7 platform: Ubuntu 18.04.4 LTS
This came with default python version 3.6.9, however I had installed my own 3.7.7 version python on it (installed building it from source)
tkinter was not working even when the help('module')
shows tkinter in the list.
The following steps worked for me:
sudo apt-get install tk-dev.
rebuild the python: 1. Navigate to your python folder and run the checks:
cd Python-3.7.7
sudo ./configure --enable-optimizations
sudo make -j 8
--- here 8 are the number of processors, check yours using nproc
command.Installing using:
sudo make altinstall
Don't use sudo make install, it will overwrite default 3.6.9 version, which might be messy later.
python3.7 -m tkinter
A windows box will pop up, your tkinter is ready now.
If you use Arch Linux (distributions like Manjaro
or Antegros
) simply type:
sudo pacman -S tk
And all will work perfectly!
You can change the matplotlib using backend using the from agg
to Tkinter TKAgg
using command
matplotlib.use('TKAgg',warn=False, force=True)
I added %matplotlib inline and my plot showed up in Jupyter Notebook.
When I ran into this error on Spyder, I changed from running my code line by line to highlighting my block of plotting code and running that all at once. Voila, the image appeared.
Linux Mint 19. Helped for me:
sudo apt install tk-dev
P.S. Recompile python interpreter after package install.