不显示添加到 networkx 图的一个节点的图像 [英] Image added to one node of a networkx graph is not displayed

查看:74
本文介绍了不显示添加到 networkx 图的一个节点的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将 jpg 图像添加到图形的一个特定节点.没有错误,但是图像没有显示在节点中.我尝试为图片投放广告的节点是KCT BS".

img=mpimg.imread('kctbs.jpg')G=nx.Graph()G.add_node(('KCT BS'),attr_dict={'image':'img'}) #将图片添加到名为#KCTBS的节点G.add_edges_from([("KCT BS","Placements"),("KCT BS","Courses"),("KCT BS","Faculty"),("KCT BS","Students"),("教师","核心:21")],长度=100)pos = nx.spring_layout(G, scale=10)nx.draw(G,with_labels=True,pos=pos,node_size=500, node_color='r')# G.node.items(0)G.nodes['KCT BS']['image']=img

解决方案

您不能在 networkx 中将图像绘制为节点.但是您可以在节点位置绘制图像

I tried adding a jpg image to one specific node of the graph. There is no error, but the image is not displayed in the node. The node I am attempting to ad an image is "KCT BS".

img=mpimg.imread('kctbs.jpg')
G=nx.Graph()
G.add_node(('KCT BS'),attr_dict={'image':'img'}) #added the image to the node named #KCTBS
G.add_edges_from([("KCT BS","Placements"),("KCT BS","Courses"),("KCT BS","Faculty"),("KCT BS","Students"),("Faculty","Core:21")],length=100)
pos = nx.spring_layout(G, scale=10)
nx.draw(G,with_labels=True,pos=pos,node_size=500, node_color='r')
# G.node.items(0)
G.nodes['KCT BS']['image']=img

解决方案

You can't draw an image as a node in networkx. But you can draw an image over the node on its position by matplotlib:

import networkx as nx
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread('hex.png')
G = nx.Graph()
G.add_node('KCT BS')
G.add_node('WAKA')
pos = nx.spring_layout(G, scale=10)
nx.draw(
    G,
    with_labels=True,
    pos=pos,
    node_size=500,
    node_color='r'
)

ax = plt.gca()
fig = plt.gcf()
trans = ax.transData.transform
trans2 = fig.transFigure.inverted().transform
imsize = 0.1

(x,y) = pos['KCT BS']
xx,yy = trans((x, y))
xa,ya = trans2((xx, yy))
a = plt.axes([xa-imsize/2.0, ya-imsize/2.0, imsize, imsize])
a.imshow(img)
a.set_aspect('equal')
a.axis('off')
plt.show()

It will draw a normal networkx graph (with circles as nodes) and then draw your picture on top of the existing graph picture (note that if you have transparent picture like mine, you will see the original networkx node under it).

这篇关于不显示添加到 networkx 图的一个节点的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆