나는 지금 몇 시간을 보내고 쉬울 것이라고 생각했지만 해결할 수 없었던 다음과 같은 문제가 있습니다. 기본적으로 내가하는 일은 네트워크를 그리는 것입니다. 각 노드는 0과 1 사이의 값을 갖습니다. 이제 값에 따라 각 노드에 색상을 지정합니다. 0은 빨간색 노드에 해당하고 1은 녹색 노드에 해당합니다. 다음 코드로이 작업을 수행합니다.
for x in range(gn):
G.add_node(x)
cn = value[x]
cn *= 100
if cn == 100:
cn = 99
if cn < 50:
# green to yellow
cg = math.floor(255 * (cn / 50))
cr = 255
else:
# yellow to red
cg = 255
cr = math.floor(255 * ((50 - cn % 50) / 50))
cb = 0
color_map.append('#%02x%02x%02x' % (cr, cg, cb))
그리고 나중에 코드에서 :
fig, ax = plt.subplots()
nx.draw(G, pos, width=weights, node_color=color_map, ax=ax)
plt.show()
이제 네트워크 옆에 컬러 바를 표시하여 플롯의 관찰자가 값에 해당하는 색상을 개괄적으로 볼 수 있도록합니다. 당신이 나를 도울 수 있기를 바랍니다. 미리 감사드립니다 :) (색상 막대는 0에서 1까지 시작해야합니다. 값이 0.5 이상인 노드도있는 경우에도 마찬가지입니다.)
편집 : 최소 작업 예 :
import networkx as nx
import matplotlib.pyplot as plt
import numpy
import math
gn = 20
color_map = []
G = nx.Graph()
value = numpy.random.uniform(0, 1, gn)
for x in range(gn):
G.add_node(x)
cn = value[x]
cn *= 100
if cn == 100:
cn = 99
if cn < 50:
# green to yellow
cg = math.floor(255 * (cn / 50))
cr = 255
else:
# yellow to red
cg = 255
cr = math.floor(255 * ((50 - cn % 50) / 50))
cb = 0
color_map.append('#%02x%02x%02x' % (cr, cg, cb))
pos = nx.spring_layout(G)
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
nx.draw(G, pos, node_color=color_map, ax=ax)
plt.show()
matplotlib의 사용자 지정 컬러 맵 기능을 사용하여이 작업을 좀 더 쉽게 할 수 있습니다. 예를 들어 http://matplotlib.org/examples/pylab_examples/custom_cmap.html을 참조 하십시오 .
다음은 networkx와 함께 사용하는 방법입니다.
import networkx as nx
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy
gn = 20
G = nx.Graph()
value = numpy.random.uniform(0, 1, gn)
G.add_nodes_from(range(gn))
# create colormap
cdict = {'red': ((0.0, 1.0, 1.0),
(0.5, 1.0, 1.0),
(1.0, 0.0, 0.0)),
'green': ((0.0, 0.0, 0.0),
(0.5, 1.0, 1.0),
(1.0, 1.0, 1.0)),
'blue': ((0.0, 0.0, 0.0),
(1.0, 0.0, 0.0))
}
green_yellow_red = LinearSegmentedColormap('GYR', cdict)
pos = nx.spring_layout(G)
nodes = nx.draw_networkx_nodes(G, pos, node_color=value, cmap=green_yellow_red)
# edges = nx.draw_networkx_nodes(G, pos) # no edges in this graph
plt.colorbar(nodes)
plt.axis('off')
plt.show()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다