Окрашивание краев прогулки в сетиx - PullRequest
3 голосов
/ 16 апреля 2011

Я написал простой код для генерации случайного блуждания в заданном графе G с использованием библиотеки networkx. Теперь, когда я берусь за прогулку, я хочу, чтобы края окрашивались и рисовались с использованием matplotlib .. скажем, в петля. Например: скажем, я иду от узла 1 к узлу 2 от соединительного края, я хочу, чтобы этот край был окрашен по-другому от остальных. Вот код:

def unweighted_random_walk(starting_point,ending_point, graph):
'''
starting_point: String that represents the starting point in the graph
ending_point: String that represents the ending point in the graph
graph: A NetworkX Graph object
'''
##Begin the random walk
current_point=starting_point
#current_node=graph[current_point]
current_point_neighors=graph.neighbors(current_point)
hitting_time=0

#Determine the hitting time to get to an arbitrary neighbor of the
#starting point
while current_point!=ending_point:
    #pick one of the edges out of the starting_node with equal probs
    possible_destination=current_point_neighbors[random.randint(0,current_point_neighors)]
    current_point=possible_destination
    current_point_neighbors=graph.neighbors(current_point)
    hitting_time+=1
return hitting_time

Ответы [ 2 ]

3 голосов
/ 16 апреля 2011

Вот что я использую:

def colors(G):
    colors = []
    for edge,data in G.edges_iter(data=True):
        # return a color string based on whatever property you want
        return 'red' if data['someproperty'] else 'blue'

        # alternatively you could store a 'color' key on the edge
        # return data['color']

    return colors

# When you invoke the draw command pass a list of edge colors
nx.draw_spectral(G, edge_color=colors(G))
0 голосов
/ 09 августа 2012

Это прекрасно работает ... однако, будьте осторожны, чтобы заполнить список соответствующими значениями. Модификация работающего кода:

def colors(G, attrib):
   colors = []    
   for node,data in G.nodes_iter(data=True):
     # return a color string based on whatever property you want
     if data['someproperty'] != attrib:
        colors.append('AliceBlue')
     else:
        colors.append('Crimson')
   return colors
...