python中networkx应用_使用NetworkX在Python中可視化圖形

我試圖在NetworkX中顯示圖形。我需要像這樣着色圖表:中心節點需要着色爲黑色。然後,所有更遠的節點都需要着色得更淺,但是當我運行代碼時,出現此錯誤:使用NetworkX在Python中可視化圖形

錯誤:無法將參數類型轉換爲行上的rgba數組:

nx.draw_networkx_nodes(G,pos,nodelist=p.keys(),node_size=90,

node_color=p.values(),cmap=plt.cm.Reds_r)

我認爲這個問題是:

node_color=p.values()

的代碼:

import numpy

import pandas

import networkx as nx

import unicodecsv as csv

import community

import matplotlib.pyplot as plt

# Generate the Graph

G=nx.davis_southern_women_graph()

# Create a Spring Layout

pos=nx.spring_layout(G)

# Find the center Node

dmin=1

ncenter=0

for n in pos:

x,y=pos[n]

d=(x-0.5)**2+(y-0.5)**2

if d

ncenter=n

dmin=d

""" returns a dictionary of nodes and their distance to the node

supplied as an argument. We will then use these distances

to determine colors"""

p=nx.single_source_shortest_path_length(G,ncenter)

plt.figure(figsize=(8,8))

nx.draw_networkx_edges(G,pos,nodelist=[ncenter],alpha=0.4)

nx.draw_networkx_nodes(G,pos,nodelist=p.keys(),node_size=90,

node_color=p.values(),cmap=plt.cm.Reds_r)

plt.show()

Full Traceback

Traceback (most recent call last):

File "", line 1, in

runfile('C:/Users/Desktop/Marvel/finding_key_players.py', wdir='C:/Users/Desktop/Marvel')

File "C:\Users\Anaconda33\lib\site- packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile

execfile(filename, namespace)

File "C:\Users\Anaconda33\lib\site packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile

exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

File "C:/Users/Desktop/Marvel/finding_key_players.py", line 70, in

cmap=plt.cm.Reds_r)

File "C:\Users\Anaconda33\lib\site-packages\networkx\drawing\nx_pylab.py", line 399, in draw_networkx_nodes

label=label)

File "C:\Users\Anaconda33\lib\site-packages\matplotlib\axes\_axes.py", line 3606, in scatter

colors = mcolors.colorConverter.to_rgba_array(c, alpha)

File "C:\Users\Anaconda33\lib\site-packages\matplotlib\colors.py", line 391, in to_rgba_array

if alpha > 1 or alpha < 0:

ValueError: Cannot convert argument type to rgba array

2016-02-15

D.Fig

+0

也許你要麼matplotlib或networkx的斷裂(或只是較早)的版本?我有matplotlib-1.3.1和networkx-1.11,它適用於那些。 –

+0

我升級了兩個模塊,但錯誤仍然相同。 –

+0

好的 - 爲了幫助你,我們需要完整的回溯和錯誤細節。我相信你的代碼是正確的。它可能與其中一個庫不匹配或存在問題,可能是matplotlib或numpy。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值