2016-06-19 70 views
0

Python Plotly圖書館使得能夠plot graphs or networksPython Plotly圖書館的網絡:如何爲不同顏色的節點着色

以下代碼(來自該link)是要繪製的返回與所述節點的不同屬性的辭典的函數:

def scatter_nodes(pos, labels=None, color=None, size=20, opacity=1): # pos is the dict of node positions # labels is a list of labels of len(pos), to be displayed when hovering the mouse over the nodes # color is the color for nodes. When it is set as None the Plotly default color is used # size is the size of the dots representing the nodes #opacity is a value between [0,1] defining the node color opacity L=len(pos) trace = Scatter(x=[], y=[], mode='markers', marker=Marker(size=[])) for k in range(L): trace['x'].append(pos[k][0]) trace['y'].append(pos[k][1]) attrib=dict(name='', text=labels , hoverinfo='text', opacity=opacity) # a dict of Plotly node attributes trace=dict(trace, **attrib)# concatenate the dict trace and attrib trace['marker']['size']=size trace['marker']['color']=color return trace

trace['marker']['color']=color分配相同的顏色到所有節點。

如果我用trace['marker']['color']=random.randint(500)替換這一行,它會給每個節點賦予一個隨機顏色。

我希望每個節點都有一個由字典預先確定的顏色,並將節點作爲鍵和顏色作爲值。

我該如何繼續?

回答

0

簡單的答案:只需要將color定義爲自動索引節點的顏色列表。