Reputation: 193
I'm beginner in python and I'm trying to run a python program, so I have the following code :
import networkx as nx
import tsplib95
import tsplib95.distances as distances
import matplotlib.pyplot as plt
SEED = 9876
# Press the green button in the gutter to run the script.
def draw_graph(graph, only_nodes=False):
"""
Helper method for drawing TSP (tour) graphs.
"""
fig, ax = plt.subplots(figsize=(12, 6))
func = nx.draw_networkx
if only_nodes:
func = nx.draw_networkx_nodes
func(data.node_coords, graph, node_size=25, with_labels=False, ax=ax)
if __name__ == '__main__':
data = tsplib95.load('xqf131.tsp')
# These we will use in our representation of a TSP problem: a list of
# (city, coord)-tuples.
cities = [(city, tuple(coord)) for city, coord in data.node_coords.items()]
solution = tsplib95.load('xqf131.opt.tour')
optimal = data.trace_tours(solution.tours)[0]
print('Total optimal tour length is {0}.'.format(optimal))
draw_graph(data.get_graph(), True)
When I execute my code I get the following error :
what is wrong with the draw_graph function?
If you have any idea help me.
Thank you in advance
Upvotes: 0
Views: 1379
Reputation: 11496
That's because the draw_networkx_nodes
doesn't have the keyword argument with_labels
as the error message says. You can use a dict to store the keyword arguments and update it accordingly:
def draw_graph(graph, only_nodes=False):
fig, ax = plt.subplots(figsize=(12, 6))
kwargs = dict(node_size=25, ax=ax)
if only_nodes:
func = nx.draw_networkx_nodes
else:
func = nx.draw_networkx
kwargs.update(with_labels=False)
func(data.node_coords, graph, **kwargs)
Upvotes: 1