AttributeError: module 'backend_interagg' has no attribute 'FigureCanvas'

Question:

I am using verision 3.6.0 of matplotlib and version 2.6.3 of networkx
and for some reason my code is giving me AttributeError: module 'backend_interagg' has no attribute 'FigureCanvas' as an error.

Code:


import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import matplotlib

G = nx.DiGraph()
nodes = np.arange(0, 8).tolist()
G.add_nodes_from(nodes)
G.add_edges_from([(0, 1), (0, 2),
                  (1, 3), (1, 4),
                  (2, 5), (2, 6), (2, 7)])
pos = {0: (10, 10),
       1: (7.5, 7.5), 2: (12.5, 7.5),
       3: (6, 6), 4: (9, 6),
       5: (11, 6), 6: (14, 6), 7: (17, 6)}
labels = {0: "CEO",
          1: "Team A Lead", 2: "Team B Lead",
          3: "Staff A", 4: "Staff B",
          5: "Staff C", 6: "Staff D", 7: "Staff E"}
nx.draw_networkx(G, pos=pos, labels=labels, arrows=True,
                 node_shape="s", node_color="white")
plt.title("Organogram of a company.")
plt.savefig("Output/plain organogram using networkx.jpeg",
            dpi=300)

Full error message:

C:UsersFlowAppDataLocalProgramsPythonPython39python.exe C:/Users/Flow/Shirt/Test.py
Traceback (most recent call last):
  File "C:UsersFlowShirtTest.py", line 21, in <module>
    nx.draw_networkx(G, pos=pos, labels=labels, arrows=True,
  File "C:UsersFlowAppDataLocalProgramsPythonPython39libsite-packagesnetworkxdrawingnx_pylab.py", line 333, in draw_networkx
    draw_networkx_nodes(G, pos, **node_kwds)
  File "C:UsersFlowAppDataLocalProgramsPythonPython39libsite-packagesnetworkxdrawingnx_pylab.py", line 445, in draw_networkx_nodes
    ax = plt.gca()
  File "C:UsersFlowAppDataLocalProgramsPythonPython39libsite-packagesmatplotlibpyplot.py", line 2225, in gca
    return gcf().gca()
  File "C:UsersFlowAppDataLocalProgramsPythonPython39libsite-packagesmatplotlibpyplot.py", line 830, in gcf
    return figure()
  File "C:UsersFlowAppDataLocalProgramsPythonPython39libsite-packagesmatplotlib_apideprecation.py", line 454, in wrapper
    return func(*args, **kwargs)
  File "C:UsersFlowAppDataLocalProgramsPythonPython39libsite-packagesmatplotlibpyplot.py", line 771, in figure
    manager = new_figure_manager(
  File "C:UsersFlowAppDataLocalProgramsPythonPython39libsite-packagesmatplotlibpyplot.py", line 346, in new_figure_manager
    _warn_if_gui_out_of_main_thread()
  File "C:UsersFlowAppDataLocalProgramsPythonPython39libsite-packagesmatplotlibpyplot.py", line 336, in _warn_if_gui_out_of_main_thread
    if (_get_required_interactive_framework(_get_backend_mod()) and
  File "C:UsersFlowAppDataLocalProgramsPythonPython39libsite-packagesmatplotlibpyplot.py", line 206, in _get_backend_mod
    switch_backend(dict.__getitem__(rcParams, "backend"))
  File "C:UsersFlowAppDataLocalProgramsPythonPython39libsite-packagesmatplotlibpyplot.py", line 266, in switch_backend
    canvas_class = backend_mod.FigureCanvas
AttributeError: module 'backend_interagg' has no attribute 'FigureCanvas'

Process finished with exit code 1
Asked By: Flow

||

Answers:

This is a fairly common issue with many causes (edited)
tldr matplotlib can’t find a backend that supports canvas drawing
This usually happens on OSX (where tkinter might not be linked due to how OSX does applications) or linux (where tkinter might not be installed because it comes separately and not by default)
try setting a backend manually, since I am on windows, use matplotlib.use('TkAgg')

Answered By: Flow
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.