Graph 创建以图像为节点的图形

Graph 创建以图像为节点的图形,graph,networkx,Graph,Networkx,我正在创建一个以节点为图像的图形 #来自 我想创建一个圆形布局,节点zero位于中心。Egdelist是[(0,1),(0,2),(0,3),(0,4),(0,5)] 但是,在输出中我发现了额外的边(附加了快照)。有没有办法删除这些额外的边?我只想要这些连接Egdelist是[(0,1),(0,2),(0,3),(0,4),(0,5)]。而且,原始图像不会显示在节点中。 有什么建议吗?这里有两个问题。第一个问题是为什么图形的边比您想要的多。发生这种情况是因为您使用了nx.complete\u

我正在创建一个以节点为图像的图形

#来自

我想创建一个圆形布局,节点
zero
位于中心。Egdelist是[(0,1),(0,2),(0,3),(0,4),(0,5)]

但是,在输出中我发现了额外的边(附加了快照)。有没有办法删除这些额外的边?我只想要这些连接Egdelist是[(0,1),(0,2),(0,3),(0,4),(0,5)]。而且,原始图像不会显示在节点中。


有什么建议吗?

这里有两个问题。第一个问题是为什么图形的边比您想要的多。发生这种情况是因为您使用了
nx.complete\u 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()

你能解释一下这两行吗
trans=ax.transData.transform
trans2=fig.transFigure.inversed().transform
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()