gpt4 book ai didi

python - networkx 绘制函数的图例

转载 作者:太空宇宙 更新时间:2023-11-04 07:36:52 25 4
gpt4 key购买 nike

所以我有以下功能来绘制我正在处理的问题。它基本上是一个关键节点检测问题或拦截。我有一些值 x,并决定攻击节点 z。基本上我想用事件和非事件节点以及正在处理/攻击的节点为我的图表着色。这是我目前所拥有的。

def draw_solution(g, zsolution, xsolution, T, xmin=0, filename='test.pdf'):

# draw solution
pos = {n: ndata['coord'] for n,ndata in g.nodes_iter(data=True)}
ncolour=[]
for n,ndata in g.nodes_iter(data=True):
if ndata['fuel_load'] < xmin:
ncolour.append('gold')
else:
ncolour.append('yellowgreen')
pp = PdfPages(filename)
fig = plt.figure(figsize=(11.7,8.3))
fig.suptitle('full graph and initial fuel load')
plt.axis('off')
nx.draw_networkx(g, pos, font_size=9, node_color=ncolour)
fig.savefig(pp, format='pdf')
fig.clf()

for t in range(T):
g_copy = g.copy()
#node colour
ncolour=[]
for i in g.nodes_iter():
if zsolution[i,t] > 0.99:
ncolour.append('lightcoral')
#g_copy.remove_edges_from(g.edges(i))
elif xsolution[i,t] < xmin:
ncolour.append('gold')
g_copy.remove_edges_from(g.edges(i))
else:
ncolour.append('yellowgreen')

plt.axis('off')
fig.suptitle('t={}, before treatment'.format(t))
nx.draw_networkx(g_copy, pos, font_size=9, node_color=ncolour, label=ncolour)
fig.savefig(pp, format='pdf')
fig.clf()

g_copy = g.copy()
#node colour
ncolour=[]
for i in g.nodes_iter():
if zsolution[i,t] > 0.99:
ncolour.append('lightcoral')
g_copy.remove_edges_from(g.edges(i))
elif xsolution[i,t] < xmin:
ncolour.append('gold')
g_copy.remove_edges_from(g.edges(i))
else:
ncolour.append('yellowgreen')

plt.axis('off')
fig.suptitle('t={}, after treatment'.format(t))
nx.draw_networkx(g_copy, pos, font_size=9, node_color=ncolour, label=ncolour)
fig.savefig(pp, format='pdf')
fig.clf()

pp.close()
fig.clf()

出于某种原因我无法正确理解图例?

求助!谢谢!

最佳答案

这就是你想要的吗?

import networkx as nx
import matplotlib.pyplot as plt

G = nx.fast_gnp_random_graph(20,0.2)

rednodes = [1,2,4,5]
bluenodes = [10,12]
greennodes = [3,6,9]
yellowgreennodes = [node for node in G.nodes() if
node not in rednodes + greennodes + bluenodes]
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos=pos, nodelist=rednodes,
node_color='red', label='red nodes')
nx.draw_networkx_nodes(G, pos=pos, nodelist=bluenodes,
node_color='blue', label='blue nodes')
nx.draw_networkx_nodes(G, pos=pos, nodelist=greennodes,
node_color='green', label='green nodes')
nx.draw_networkx_nodes(G, pos=pos, nodelist=yellowgreennodes,
node_color='yellowgreen', label='yellowgreen nodes')
nx.draw_networkx_edges(G, pos=pos)
plt.legend(scatterpoints = 1)
plt.show()

enter image description here

关于python - networkx 绘制函数的图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32931484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com