Skip to content

Instantly share code, notes, and snippets.

@matthewfeickert
Last active April 21, 2021 02:02
Show Gist options
  • Save matthewfeickert/84245837f09673b2e7afea929c016904 to your computer and use it in GitHub Desktop.
Save matthewfeickert/84245837f09673b2e7afea929c016904 to your computer and use it in GitHub Desktop.
Examples of non-interactive matplotlib backends with or without GUIs

Examples of non-interactive matplotlib backends with or without GUIs

It is first worth reading about backends in the matplotlib Usage Guide.

Non-interactive with GUI

Running

$ python with_gui.py
Interactive mode: False
matplotlib backend: TkAgg

will run load and show the GUI without stopping and then close it when the script finishes. Running

$ python -i with_gui.py

will keep the REPL running after the script executes and allow for further interaction and updating of the GUI.

Non-interactive without GUI

GUIs can be avoided by not using the GUI compatible matplotlib.pyplot APIs and using the matplotlib.figure.Figure APIs instead.

$ python without_gui.py
Interactive mode: False
matplotlib backend: TkAgg
Traceback (most recent call last):
  File "without_gui.py", line 17, in <module>
    fig.show()  # Errors as no GUI available
  File "/home/feickert/.pyenv/versions/base/lib/python3.8/site-packages/matplotlib/figure.py", line 407, in show
    raise AttributeError(
AttributeError: Figure.show works only for figures managed by pyplot, normally created by pyplot.figure()

Non-GUI backend (no GUI possible)

Or, as recommended by Thomas Caswell, set a non-GUI backend so that the GUI frameworks aren't even imported.

$ python nongui_backend.py
Interactive mode: False
matplotlib backend: agg
nongui_backend.py:20: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
  fig.show()  # Errors as no GUI possible
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
if __name__ == "__main__":
print(f"Interactive mode: {matplotlib.is_interactive()}")
# Agg is non-interactive and will not even import the GUI frameworks
# c.f. https://matplotlib.org/stable/tutorials/introductory/usage.html#what-is-a-backend
# c.f. https://twitter.com/tacaswell/status/1377122080664166408
matplotlib.use("agg")
print(f"matplotlib backend: {matplotlib.rcParams['backend']}")
x = np.linspace(0, 10, 1000)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
fig.savefig("nongui_backend.png")
fig.show() # Errors as no GUI possible
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
if __name__ == "__main__":
print(f"Interactive mode: {matplotlib.is_interactive()}")
print(f"matplotlib backend: {matplotlib.rcParams['backend']}")
x = np.linspace(0, 10, 1000)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
fig.savefig("with_gui.png")
fig.show() # Works as GUI available
import matplotlib
import numpy as np
from matplotlib.figure import Figure
if __name__ == "__main__":
print(f"Interactive mode: {matplotlib.is_interactive()}")
print(f"matplotlib backend: {matplotlib.rcParams['backend']}")
x = np.linspace(0, 10, 1000)
y = np.sin(x)
fig = Figure()
ax = fig.subplots()
ax.plot(x, y)
fig.savefig("without_gui.png")
fig.show() # Errors as no GUI available
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment