Q: NetworkX: Node labels are drawn randomly So I wanted to draw labels for each of my node (1-20), but somehow these labels are just drawn randomly on the graph (check screen below).
network = nx.DiGraph()
counter = 0
for i in range(1,21):
    network.add_node(i, label = str(i))
with open('mreza.csv', newline='') as csvfile:
    reader = csv.reader(csvfile, delimiter=';', quotechar='|')
    for row in reader:
        for item in row:
            item = item.strip('(').strip(')').split(',')
            startVertex = int(item[0])
            endVertex = int(item[1])
            network.add_weighted_edges_from([(startVertex, endVertex, matchingArray[counter])])
            counter = counter + 1
pos=nx.spring_layout(network)
nx.draw(network)
labels = nx.draw_networkx_labels(network,pos,font_size=10)
plt.show()

nan

A: There are a few issues in your code:


*

*You create nodes and edges separately. The labels that you think are random are associated with nodes that you have created in the first loop (I guess). 

*draw can be called with extra argument to draw labels, i.e. nx.draw(G, pos=pos, with_labels=True, font_size=10).

*add_weighted_edges_from is supposed to be given a container of edges. You're sort of abusing this function to add edges. If you insist on going with a loop, consider using add_edge.


Considering that you load your data from csv, I'd not go into loops at all. Instead, I'd load my data into pandas dataframe and then use from_pandas_edgelist like this:
import pandas as pd
import networkx as nx

df = pd.read_csv('mreza.csv')
G = nx.from_pandas_edgelist(df, create_using=nx.DiGraph)
pos = nx.spring_layout(G)
nx.set_node_attributes(G, pos, 'pos')

Mind that your df needs to have two mandatory columns: source and target. Pre-process your df if necessary. Add weight and you're done.
