2017-03-09 14 views
-1
new = (('AXIN', 37, REPORTED), 
('LGR', 34, REPORTED), 
('NKD', 29, REPORTED), 
('TNFRSF', 23, REPORTED), 
('APCDD', 18, REPORTED), 
('TOX', 15, UNREPORTED), 
('LEF', 14, REPORTED), 
('PLCB', 13, REPORTED), 
('MME', 13, UNREPORTED), 
('NOTUM', 13,UN REPORTED), 
('GNG', 11, , REPORTED), 
('LOXL', 10, UNREPORTED)) 

import matplotlib.pyplot as plt 
import networkx as nx 
children = sorted(new, key=lambda x: x[1]) 
parent = children.pop()[0] 

G = nx.Graph() 
for child, weight in children: G.add_edge(parent, child, weight=weight) 
width = list(nx.get_edge_attributes(G, 'weight').values()) 
plt.savefig("plt.gene-expression.pdf") 
plt.figure(figsize = (20, 10)) 

nx.draw_networkx(G, font_size=10, node_size=2000, alpha=0.6) #width=width is very fat lines 
plt.savefig("gene-expression-graph.pdf") 

このnxグラフでは、どのようにして未登録 - 緑色、REPORTED-黄色にすることができますか? 親ノードが最大数、すなわち、AXINのノード、37python networkX:タプルからグラフを作成し、ノードに異なる色を割り当てる

答えて

0
colors = [] 
for i in new: 
     if i[2] == 'UNREPORTED': 
       colors.append('green') 
     elif i[2] == 'REPORTED': 
       colors.append('yellow') 
nx.draw_networkx(G, font_size=10, node_size=2000, alpha=0.6, node_color=colors) 
+0

必須ではないが、次の同じコードを実行するとaxinが黄色のノードとして表示されます。 –

+0

@Bonlenfum 皆さん、どうやってこのnx質問を解決できるか教えてください。 http://stackoverflow.com/questions/43090538/draw-common-friends-connections-of-three-people-using-networkx –

0

ある順序におけるミスマッチはnetworkxのグラフ表現の根底にある辞書から来ます。色のリストが正しい方法で並べられていることを確認したら、正しいノードに適切な色を付けます。 私はあなたが望むと思うものを達成するためにここに2つの異なるアプローチを書いてきました。

注:すべてのタプルの3番目の部分を文字列に変換するのではなく、報告されたものと報告されていないものの値を宣言しました。 、緑のノード - 例えば 、コードの最初のルーンは私にアキシンを与える:しかし、この部分は、それがノードIDを見ずにノードに黄色と緑の色を割り当て

# Delcare the graph: 
REPORTED = 1 
UNREPORTED = 2 

new = (('AXIN', 37, REPORTED), 
('LGR', 34, REPORTED), 
<...> 
('LOXL', 10, UNREPORTED)) 

# 2 axes to show different approaches 
plt.figure(1); plt.clf() 
fig, ax = plt.subplots(1, 2, num=1, sharex=True, sharey=True) 

### option 1: draw components step-by-step 
# positions for drawing of all components in right place 
pos = nx.spring_layout(G) 

# identify which nodes are reported/unreported 
nl_r = [name for (name, w, state) in new if state == REPORTED] 
nl_u = [name for (name, w, state) in new if state == UNREPORTED] 

# draw each subset of nodes in relevant color 
nx.draw_networkx_nodes(G, pos=pos, nodelist=nl_r, node_color='g', nodesize=2000, ax=ax[0]) 
nx.draw_networkx_nodes(G, pos=pos, nodelist=nl_u, node_color='y', nodesize=2000, ax=ax[0]) 
# also need to draw the egdes 
nx.draw_networkx_edges(G, pos=pos, ax=ax[0]) 
nx.draw_networkx_labels(G, pos=pos, ax=ax[0], font_size=10) 

### option 2: more complex color list construction (but simpler plot command) 
nl, cl = zip(*[(name, 'g') if state == REPORTED else (name, 'y') for (name, w, state) in new]) 

nx.draw_networkx(G, pos=pos, nodelist=nl, node_color=cl, nodesize=2000, ax=ax[1], font_size=10) 

plt.show() 

example output

関連する問題