简体   繁体   中英

got an unexpected keyword argument 'with_labels'

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM