I try to run the following simple code on Python importing Matplotlib:
import matplotlib.pyplot as plt
import numpy as np
# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks')
ax.grid()
fig.savefig("test.png")
plt.show()
but I get the following output:
Traceback (most recent call last):
File "/home/megamero/Codes/Python/Tkinter/matplot.py", line 8, in <module>
fig, ax = plt.subplots()
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 1176, in subplots
fig = figure(**fig_kw)
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 539, in figure
**kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/backend_bases.py", line 171, in new_figure_manager
return cls.new_figure_manager_given_figure(num, fig)
File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 1049, in new_figure_manager_given_figure
window = Tk.Tk(className="matplotlib")
File "/usr/lib/python3.6/tkinter/__init__.py", line 2023, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
I have to mention, that yesterday I tried to run Matplotlib with Tkinter, but I couldn't because, when trying to import NavigationToolbar2Tk as from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk
I got an error: ImportError: cannot import name 'NavigationToolbar2Tk
.
I believe that, when I tried to replace that line with matplotlib.use("TkAgg")
-solution that didn't work- Matplotlib configuration had been changed and that's why I can't even run a simple plot without any external GUI. I am using Visual Studio Code with WSL: Ubuntu-18.04, and Python3. Any help or idea is welcome.