在 Networkx Graph 上绘制边值
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()
当我绘制图形时,边缘上的标签(转移矩阵从 1 状态转移到另一状态的概率值)没有出现,有人知道如何设置它们吗?
我正在尝试使用以下无法运行的代码
pos = nx.spring_layout(G)
edge_labels = nx.draw_networkx_edge_labels(G, pos)
我不确定什么是“不起作用”,但您应该对图表的两个部分使用相同的 pos
:
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)
输出:
我正在使用 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()
当我绘制图形时,边缘上的标签(转移矩阵从 1 状态转移到另一状态的概率值)没有出现,有人知道如何设置它们吗?
我正在尝试使用以下无法运行的代码
pos = nx.spring_layout(G)
edge_labels = nx.draw_networkx_edge_labels(G, pos)
我不确定什么是“不起作用”,但您应该对图表的两个部分使用相同的 pos
:
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)
输出: