4

Expected Behavior (local environment: fresh MacOS 12.4 installation)

With no environment updates except $ pip3 install matplotlib, I can successfully run this simple plot from the Matplotlib documentation:

Example Code:
# testplot.py
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()
Actual Output (saved to a .png after window opens):

Run $ python3 testplot.py in the terminal:

enter image description here

Observed Behavior (vscode python 3.8 dev container)

Disclaimer: This post does not address notebook-based plots (which work fine but are not always preferred)

However, when I run this in my dev container, I get the following error:

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

First Attempted Solution:

Following this previously posted solution, I specified the backend (export MPLBACKEND=TKAgg) before running the interpreter, but the error persists.

Second Attempted Solution:

Following the comments, I added the following lines to the script:

import matplotlib
matplotlib.use('tkagg')

In the v3.8 dev container, this addition changes the error to:

Traceback (most recent call last):
  File "testplot.py", line 5, in <module>
    matplotlib.use('tkagg')
  File "/usr/local/python/lib/python3.8/site-packages/matplotlib/__init__.py", line 1144, in use
    plt.switch_backend(name)
  File "/usr/local/python/lib/python3.8/site-packages/matplotlib/pyplot.py", line 296, in switch_backend
    raise ImportError(
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running

Note: adding these two lines broke the local script as well. The point of the local example was to show that it plots stuff without installing anything except matplotlib.

mkk
  • 879
  • 6
  • 19
  • did you try `matplotlib.use(...)`? – Paul H May 27 '22 at 22:48
  • thanks @PaulH, I've added that case to the attempted solutions – mkk May 27 '22 at 22:59
  • Ok -- so what's your interpretation of that error message? – Paul H May 27 '22 at 23:00
  • my first reaction was to run `pip freeze` and check whether `tk` was installed, and confirmed that it was via the following listing: `tk==0.1.0`. aside from that I just googled "tk vs headless python" and didn't find anything helpful. – mkk May 27 '22 at 23:07
  • if you're running in a container -- there's no GUI framework (head) to interact with, right? – Paul H May 27 '22 at 23:08
  • yeah that's a good point. although I don't know much about python gui framework, I did check `echo $DISPLAY && xclock` and got an error even though `DISPLAY` is set properly, I think this may be a bug under https://github.com/microsoft/vscode-remote-release/issues/6184 – mkk May 27 '22 at 23:40
  • For windows machine see this answer: https://stackoverflow.com/a/73375874/6274417 – user6274417 Aug 16 '22 at 14:44

0 Answers0