2

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.

7
  • did you try matplotlib.use(...)? Commented May 27, 2022 at 22:48
  • thanks @PaulH, I've added that case to the attempted solutions Commented May 27, 2022 at 22:59
  • Ok -- so what's your interpretation of that error message? Commented May 27, 2022 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. Commented May 27, 2022 at 23:07
  • if you're running in a container -- there's no GUI framework (head) to interact with, right? Commented May 27, 2022 at 23:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.