【问题标题】:python networkX: Making graph from tuples and assigning different colour for nodespython networkX:从元组制作图并为节点分配不同的颜色
【发布时间】:2017-03-13 20:19:18
【问题描述】:
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 图中,我怎样才能使 UNREPORTED - 绿色,REPORTED - 黄色? 父节点是编号最大的节点,即AXIN,37

【问题讨论】:

    标签: tuples python-3.5 networkx


    【解决方案1】:
    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)
    

    【讨论】:

    • 它在不查看节点 ID 的情况下为节点分配黄色和绿色:例如,第一个符文代码给我 axin - 绿色节点,下一次运行相同代码给我 axin 作为黄色节点.
    • @Bonlenfum 伙计们,你能提出我如何解决这个 nx 问题的建议吗? stackoverflow.com/questions/43090538/…
    【解决方案2】:

    排序的不匹配来自于 networkx 图形表示的基础字典。如果您确保颜色列表的排序方式与您将获得正确节点的正确颜色相同的方式。 我在这里写了两种不同的方法来实现我认为你想要的。

    注意:我声明了已报告和未报告的值,而不是将每个元组的第三部分转换为字符串。但这部分不是必需的

    # 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()
    

    【讨论】:

      猜你喜欢
      • 2012-11-11
      • 2021-04-30
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      • 2021-11-27
      • 1970-01-01
      相关资源
      最近更新 更多