Search code examples
pythonmatplotlibmachine-learningnetworkxvehicle-routing

Matplotlib - How to plot with coordinates of the nodes?


I am currently solving a vehicle routing problem and i want to visualise my solution base on the scatter plot. However, how do I connect lets say for example Node1 to Node 5?

I want to connect to a particular node base on my solution.

enter image description here

edit: I have tried using matplotlib as mentioned, but i can't get the coordinates.

enter image description here

The code is written as stated below.

import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore", category=UserWarning)

# G = nx.Graph()
G = nx.DiGraph(directed=True)
G.add_edges_from(
    [('0', '3'), ('3', '6'), ('6', '0'), ('0', '5'), ('5', '4'),
     ('4', '0'), ('0', '2'), ('2', '1'), ('1', '0')])

val_map = {'1': 1.0,
           '5': 0.5714285714285714,
           '6': 0.0}

values = [val_map.get(node, 0.25) for node in G.nodes()]

# nx.draw(G, cmap = plt.get_cmap('jet'), node_color = values)
# nx.draw_networkx(G[, pos, arrows, with_labels])

options = {
    'node_color': 'green',
    'node_size': 800,
    'width': 3,
    'arrowstyle': '-|>',
    'arrowsize': 20,
}
nx.draw_networkx(G, arrows=True, **options)

plt.show()

Also, the coordinates of the nodes are given as follows from node0-6 respectively:

data['locations'] = [(1106, 3368.76),
(97.497, 230.937), (55.514, 2920.53),  
(44.019, 5588.47), (2499.09, 242.61),
(2652.1, 2932.21), (2640.87, 5615.41),]
    

PS. I have another problem with this is that the graph kepts changing whenever I run it. How do I make it fix?


Solution

  • documentation of draw_networkx

    Parameters
    G graph
    A networkx graph
    
    pos dictionary, optional
    A dictionary with nodes as keys and positions as values. If not specified a 
    spring layout positioning will be computed. See
    networkx.drawing.layout for functions that compute node positions.
    

    You need to specify the pos keyword argument in nx.draw_networkx to add node positions, if you do not, then a new layout will be generated each time you draw the graph.

    pos needs to be a dictionary, with node names as keys, and node coordinates as values.

    import networkx as nx
    import numpy as np
    import matplotlib.pyplot as plt
    
    G = nx.DiGraph(directed=True)
    G.add_edges_from(
        [('0', '3'), ('3', '6'), ('6', '0'), ('0', '5'), ('5', '4'),
         ('4', '0'), ('0', '2'), ('2', '1'), ('1', '0')])
    
    val_map = {'1': 1.0,
               '5': 0.5714285714285714,
               '6': 0.0}
    
    values = [val_map.get(node, 0.25) for node in G.nodes()]
    
    options = {
        'node_color': 'green',
        'node_size': 800,
        'width': 3,
        'arrowstyle': '-|>',
        'arrowsize': 20,
    }
    
    
    # creating a variable for the locations, as "data" was undefined
    locations = [(1106, 3368.76),
    (97.497, 230.937), (55.514, 2920.53),  
    (44.019, 5588.47), (2499.09, 242.61),
    (2652.1, 2932.21), (2640.87, 5615.41),]
    
    # generating pos dictionary
    pos = {str(i):location for i, location in enumerate(locations)}
    
    # drawing graph, with positions included.  
    nx.draw_networkx(G, pos=pos, arrows=True, **options)
    
    plt.show()