[Numpy-discussion] numpy array in networkx graph?

Brett Olsen brett.olsen@gmail....
Tue Jun 12 14:32:19 CDT 2012


This seems to work:

import networkx as nx
import pylab
import numpy as N

M = N.random.random((10, 10))
G = nx.Graph(M)
node_colors = []
for i in xrange(len(M)):
  if M[i,0] < 0.5:
    node_colors.append('white')
  else:
    node_colors.append('blue')
nx.draw(G, node_color=node_colors)
pylab.show()

~Brett

On Tue, Jun 12, 2012 at 1:49 PM, bob tnur <bobtnur78@gmail.com> wrote:
> can anyone give me a hint on the following code?
>
> import network as nx
> import pylab as plt
>
> G=nx.Graph(M)  # M is numpy matrix ,i.e:type(M)=numpy.ndarray
> for i in xrange(len(M)):
>       tt=P[i,:].sum()
>       if tt==1:
>           G.add_node(i,color='blue')
>       elif tt==2:
>           G.add_node(i,color='red')
>       elif tt==3:
>           G.add_node(i,color='white')
>       else:
>           tt==4
>           G.add_node(i,color='green')
> G.nodes(data=True)
> T=nx.draw(G)
> plt.axis('off')
> plt.savefig("test.png")
>
> I didn't get color change, still the defualt color is used.Did I miss
> something?
>
> my aim is to obtain:
> something like:
> find total number of w-red-red-z path
>            number of w-red-red-red-z path
>            number of w-red-red-red-red-z path
> where w (left side of some cyclic polygon(can also be conjugated ring))
> and z(right-side of it)are any of the colors except red.
>
> any comment is appreciated?
> Thanks
> Bob
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>


More information about the NumPy-Discussion mailing list