我正在创建一个以节点为图像的图形,
# 图片来自http://matplotlib.sourceforge.net/users/image_tutorial.html
我想创建一个圆形布局,节点zero
位于中心。Egdelist 是 [(0,1),(0,2),(0,3),(0,4),(0,5)]
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import networkx as nx
img=mpimg.imread('stinkbug.png')
G=nx.complete_graph(6)
G.node[0]['image']=img
G.node[1]['image']=img
G.node[2]['image']=img
G.node[3]['image']=img
G.node[4]['image']=img
G.node[5]['image']=img
print(G.nodes())
G.add_edge(0,1)
G.add_edge(0,2)
G.add_edge(0,3)
G.add_edge(0,4)
G.add_edge(0,5)
print(G.edges())
nx.draw_circular(G)
Run Code Online (Sandbox Code Playgroud)
但是,在输出中我找到了额外的边(附有快照)。有没有办法删除这些额外的边?我只想要这些连接 Egdelist 是 [(0,1),(0,2),(0,3),(0,4),(0,5)]。此外,原始图像不显示在节点中.
有什么建议?
所以这里真的有两个问题。第一个是为什么你的图的边比你想要的多。发生这种情况是因为您曾经nx.complete_graph(6)
初始化您的图 - 这会在 6 个节点上创建一个完整的图。您应该初始化一个空图,使用图像元数据添加节点,然后添加边。
为了将节点绘制为您的图像,我从本讨论中找到并稍微修改了代码。它有一些您可以自定义的内容,例如图像大小。结果是:
希望这可以帮助!
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import networkx as nx
img=mpimg.imread('/Users/johanneswachs/Downloads/stink.jpeg')
G=nx.Graph()
G.add_node(0,image= img)
G.add_node(1,image= img)
G.add_node(2,image= img)
G.add_node(3,image= img)
G.add_node(4,image= img)
G.add_node(5,image= img)
print(G.nodes())
G.add_edge(0,1)
G.add_edge(0,2)
G.add_edge(0,3)
G.add_edge(0,4)
G.add_edge(0,5)
print(G.edges())
pos=nx.circular_layout(G)
fig=plt.figure(figsize=(5,5))
ax=plt.subplot(111)
ax.set_aspect('equal')
nx.draw_networkx_edges(G,pos,ax=ax)
plt.xlim(-1.5,1.5)
plt.ylim(-1.5,1.5)
trans=ax.transData.transform
trans2=fig.transFigure.inverted().transform
piesize=0.2 # this is the image size
p2=piesize/2.0
for n in G:
xx,yy=trans(pos[n]) # figure coordinates
xa,ya=trans2((xx,yy)) # axes coordinates
a = plt.axes([xa-p2,ya-p2, piesize, piesize])
a.set_aspect('equal')
a.imshow(G.node[n]['image'])
a.axis('off')
ax.axis('off')
plt.show()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4870 次 |
最近记录: |