-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")

In this nx graph, how can I make the UNREPORTED - green color, REPORTED-yellow color? Parent node is the node with the largest number i.e., AXIN, 37

J.A
  • 204
  • 1
  • 4
  • 13

2 Answers2

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)
st.ph.n
  • 549
  • 2
  • 5
  • 19
  • it assigns yellow and green colours to the node without looking at node ID: For instance, first rune of code gives me axin - green node, the next run of the same code gives me axin as yellow node. – J.A Mar 10 '17 at 11:13
  • @Bonlenfum Guys, could you give suggestions how I can solve this nx question? http://stackoverflow.com/questions/43090538/draw-common-friends-connections-of-three-people-using-networkx – J.A Mar 29 '17 at 11:03
0

The mismatch in ordering comes from the dictionaries that underlie networkx's graph representation. If you ensure that the list of colors is ordered the same way you will have the right color for the right node. I've written two different approaches here that achieve what I think you want.

Note: I declared values for reported and unreported, rather than turning the third piece of every tuple into a string. But this part isn't essential

# 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

Bonlenfum
  • 19,101
  • 2
  • 53
  • 56