[英]Drawing edges value on Networkx Graph
我正在使用 .networkx 使用以下代碼繪制馬爾可夫決策過程圖
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()
當我繪制圖形時,邊緣上的 label(從 1 state 轉移到轉移矩陣的另一個存在的概率值)沒有出現,有人知道如何設置它們嗎?
我正在嘗試使用以下無法正常工作的代碼
pos = nx.spring_layout(G)
edge_labels = nx.draw_networkx_edge_labels(G, pos)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.