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:\Users\Flow\AppData\Local\Programs\Python\Python39\python.exe C:/Users/Flow/Shirt/Test.py
Traceback (most recent call last):
File "C:\Users\Flow\Shirt\Test.py", line 21, in <module>
nx.draw_networkx(G, pos=pos, labels=labels, arrows=True,
File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\networkx\drawing\nx_pylab.py", line 333, in draw_networkx
draw_networkx_nodes(G, pos, **node_kwds)
File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\networkx\drawing\nx_pylab.py", line 445, in draw_networkx_nodes
ax = plt.gca()
File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 2225, in gca
return gcf().gca()
File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 830, in gcf
return figure()
File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\_api\deprecation.py", line 454, in wrapper
return func(*args, **kwargs)
File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 771, in figure
manager = new_figure_manager(
File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 346, in new_figure_manager
_warn_if_gui_out_of_main_thread()
File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 336, in _warn_if_gui_out_of_main_thread
if (_get_required_interactive_framework(_get_backend_mod()) and
File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 206, in _get_backend_mod
switch_backend(dict.__getitem__(rcParams, "backend"))
File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.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
This is a fairly common issue with many causes
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')