0

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.

  • Have you tried the solution in this question? https://stackoverflow.com/questions/43397162/show-matplotlib-plots-and-other-gui-in-ubuntu-wsl1-wsl2 – Nick ODell Nov 07 '22 at 18:51
  • Does this answer your question? [Show matplotlib plots (and other GUI) in Ubuntu (WSL1 & WSL2)](https://stackoverflow.com/questions/43397162/show-matplotlib-plots-and-other-gui-in-ubuntu-wsl1-wsl2) – Jody Klymak Nov 07 '22 at 21:34

1 Answers1

1

Create a virtual environment with Numpy and Matplotlib only and see if it works. You could be dealing with hairy package dependency issues here and trust me you don't want to waste your time untangling the mess...

Linux/MacOs, open a new terminal:

user@macbook % mkdir my_matplotlib_project
user@macbook % cd my_matplotlib_project
user@macbook my_matplotlib_project % pip install pipenv
(my_matplotlib_project) user@macbook my_matplotlib_project %  pipenv shell
(my_matplotlib_project) user@macbook my_matplotlib_project % pipenv install numpy matplotlib

While keeping terminal open, go to any text editor create file main.py and copy your code. Finally, back to terminal.

(my_matplotlib_project) user@macbook my_matplotlib_project % python main.py

This virtual environment is now dedicated to this project. You may exit the virtual environment:

(my_matplotlib_project) user@macbook my_matplotlib_project % exit

The text (my_matplotlib_project) indicating you are in a virtual environment will now disappear from your terminal.

You will avoid plenty of issues by adopting virtual environments in your workflow. Learn more here https://pypi.org/project/pipenv/

hknjj
  • 319
  • 1
  • 8