Drawing edges value on Networkx Graph

704 views Asked by At

I am using networkx to draw the graph of a Markov Decision Process with the following code

import numpy as np
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt


states = ["sleeping", "eating", "pooping"]
pi = [0.35, 0.35, 0.3]


state_space = pd.Series(pi, index=states, name="states")
print(state_space)
print(state_space.sum())

q_df = pd.DataFrame(columns=states, index=states)
print(q_df)

q_df.loc[states[0]] = [0.4, 0.2, 0.4]
q_df.loc[states[1]] = [0.45, 0.45, 0.1]
q_df.loc[states[2]] = [0.45, 0.25, 0.3]

print(q_df)
q = q_df.values
print("TransitionMatrix: ", "\n", q)

print("\n", q, q.shape, "\n")
print(q_df.sum(axis = 1))


from pprint import pprint

## create a funcion that maps transition probabilit yinto a dataframe
# to markov edges and weights

def _get_markov_edges(Q):
    edges = {}
    for col in Q.columns:
        for idx in Q.index:
            edges[(idx, col)] = Q.loc[idx,col]
    return edges

edges_wts = _get_markov_edges(q_df)
pprint(edges_wts)

G = nx.MultiDiGraph()

G.add_nodes_from(states)
print(f"Nodes:\n{G.nodes()}\n")


# edges represent transition probabilities

for k,v in edges_wts.items():
    tmp_origin, tmp_destination = k[0], k[1]
    G.add_edge(tmp_origin, tmp_destination, weight = v, label = v)

print(f"Edges: ")
pprint(G.edges(data=True))

nx.draw(G, with_labels= True)

plt.show()

When I am plotting the graph the label on the edges (The probability value to transit from 1 state to another present of the Transition matrix) are not appearing, someone know how to Set them?

I am trying to use the following piece of code which is not working

    pos = nx.spring_layout(G)

edge_labels = nx.draw_networkx_edge_labels(G, pos)
1

There are 1 answers

3
mozway On BEST ANSWER

I am not sure what is "not working", but you should use the same pos for both components of the graph:

pos = nx.spring_layout(G)
labels = {x[:2]: G.get_edge_data(*x)['label']  for x in G.edges}
nx.draw(G, pos, with_labels= True)
nx.draw_networkx_edge_labels(G, pos, labels)

output:

enter image description here