使用 plotly 在 Networkx 跟踪中添加边缘着色的选项
Option to add edge colouring in Networkx trace using plotly
在plotly网站给出的例子中:
https://plotly.com/python/network-graphs/
有没有办法为每条边添加边着色(例如从十六进制值列表)?
就像给节点的一样
这是不同颜色边的示例:
import matplotlib.pyplot as plt
import networkx as nx
G = nx.cubical_graph()
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, nodelist=[0, 1, 2, 3], node_color='y')
nx.draw_networkx_nodes(G, pos, nodelist=[4, 5, 6, 7], node_color='g')
nx.draw_networkx_edges(G, pos, edgelist=[(0, 1), (1, 2), (2, 3), (3, 0)], edge_color='y')
nx.draw_networkx_edges(G, pos, edgelist=[(4, 6), (6, 5), (5, 7), (7, 4)], edge_color='g')
nx.draw_networkx_labels(G, pos)
plt.show()
输出:
更多信息,请参阅 documentation。
您可以将不同颜色的边分开并单独绘制。当您为每组边调用 go.Scatter
时,只需设置您想要的颜色:
colors = ['#ff0000', '#0000ff']
edge_traces = []
for edge_set, c in zip(edge_sets, colors):
edge_x = []
edge_y = []
for edge in edge_set:
x0, y0 = G.nodes[edge[0]]['pos']
x1, y1 = G.nodes[edge[1]]['pos']
edge_x.append(x0)
edge_x.append(x1)
edge_x.append(None)
edge_y.append(y0)
edge_y.append(y1)
edge_y.append(None)
edge_traces.append(go.Scatter(
x=edge_x, y=edge_y,
line=dict(width=0.5, color=c),
hoverinfo='none',
mode='lines'))
只需确保在创建图形时添加新的散点图
fig = go.Figure(data=edge_traces + [node_trace],
...
在plotly网站给出的例子中: https://plotly.com/python/network-graphs/
有没有办法为每条边添加边着色(例如从十六进制值列表)?
就像给节点的一样
这是不同颜色边的示例:
import matplotlib.pyplot as plt
import networkx as nx
G = nx.cubical_graph()
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, nodelist=[0, 1, 2, 3], node_color='y')
nx.draw_networkx_nodes(G, pos, nodelist=[4, 5, 6, 7], node_color='g')
nx.draw_networkx_edges(G, pos, edgelist=[(0, 1), (1, 2), (2, 3), (3, 0)], edge_color='y')
nx.draw_networkx_edges(G, pos, edgelist=[(4, 6), (6, 5), (5, 7), (7, 4)], edge_color='g')
nx.draw_networkx_labels(G, pos)
plt.show()
输出:
更多信息,请参阅 documentation。
您可以将不同颜色的边分开并单独绘制。当您为每组边调用 go.Scatter
时,只需设置您想要的颜色:
colors = ['#ff0000', '#0000ff']
edge_traces = []
for edge_set, c in zip(edge_sets, colors):
edge_x = []
edge_y = []
for edge in edge_set:
x0, y0 = G.nodes[edge[0]]['pos']
x1, y1 = G.nodes[edge[1]]['pos']
edge_x.append(x0)
edge_x.append(x1)
edge_x.append(None)
edge_y.append(y0)
edge_y.append(y1)
edge_y.append(None)
edge_traces.append(go.Scatter(
x=edge_x, y=edge_y,
line=dict(width=0.5, color=c),
hoverinfo='none',
mode='lines'))
只需确保在创建图形时添加新的散点图
fig = go.Figure(data=edge_traces + [node_trace],
...