如何在 NetworkX 中的节点上显示图像图标 [英] How to display image icons on nodes in NetworkX

查看:57
本文介绍了如何在 NetworkX 中的节点上显示图像图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 NetworkX 模块来呈现基础设施系统的网络.在这个系统中,节点和边由不同类型的实体组成,我想使用一些图标来表示它们的类型,而不是圆形、星形或方形.

G = nx.Graph()G.add_edge('a', 'b')nx.draw(G, labels={'a':'img1', 'b':'img2'}, with_labels=True)plt.show()

显然,我的脚本创建了一个带有img1"和img2"标签的图表.我正在寻找一种方法来显示两个图标而不是实心圆圈和标签.

解决方案

您可以使用 networkx 创建绘图并获取图形的句柄.然后使用 matplotlib 在它上面绘图.

将 networkx 导入为 nx导入 matplotlib.pyplot 作为 plt将 matplotlib.image 导入为 mpimg导入全局从 numpy 导入 sqrt导入全局路径 = '一些/文件夹/with/png/files'files = [f for f in glob.glob(path + "*.png")]图片 = []对于文件中的 f:img.append(mpimg.imread(f))N = len(文件)# 生成图G = nx.watts_strogatz_graph(N,4,0.2)pos=nx.spring_layout(G,k=3/sqrt(N))# 在节点上绘制图像nx.draw_networkx(G,pos,width=3,edge_color="r",alpha=0.6)ax=plt.gca()图=plt.gcf()trans = ax.transData.transformtrans2 = fig.transFigure.inverted().transformimsize = 0.1 # 这是图像大小对于 G.nodes() 中的 n:(x,y) = pos[n]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[n])a.set_aspect('相等')a.axis('关闭')plt.show()

位于 https://gist.github.com/munthe/7de513dc886917860f7b960e10>

这里是一个似乎在边缘而不是节点上绘图的示例.

I am using NetworkX module to present the network of an infrastructure systems. In this systems, nodes and edges consist of different types of entities and I would like to use some icons representing their types instead of circle, star or square.

G = nx.Graph()
G.add_edge('a', 'b')
nx.draw(G, labels={'a':'img1', 'b':'img2'}, with_labels=True)
plt.show()

Obviously, my script creates a graph with labels of 'img1' and 'img2'. I am looking for a way to display two icons instead of the filled circles and labels.

解决方案

You can create a plot using networkx and get the handle to the figure. Then use matplotlib to plot ontop of it.

import networkx as nx
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
from numpy import sqrt
import glob

path = 'some/folder/with/png/files'
files = [f for f in glob.glob(path + "*.png")]
img = []
for f in files:
    img.append(mpimg.imread(f))
N = len(files)

# generate graph
G = nx.watts_strogatz_graph(N,4,0.2)
pos=nx.spring_layout(G,k=3/sqrt(N))

# draw with images on nodes
nx.draw_networkx(G,pos,width=3,edge_color="r",alpha=0.6)
ax=plt.gca()
fig=plt.gcf()
trans = ax.transData.transform
trans2 = fig.transFigure.inverted().transform
imsize = 0.1 # this is the image size
for n in G.nodes():
    (x,y) = pos[n]
    xx,yy = trans((x,y)) # figure coordinates
    xa,ya = trans2((xx,yy)) # axes coordinates
    a = plt.axes([xa-imsize/2.0,ya-imsize/2.0, imsize, imsize ])
    a.imshow(img[n])
    a.set_aspect('equal')
    a.axis('off')
plt.show()

Found at https://gist.github.com/munthe/7de513dc886917860f7b960a51c95e10

Here is an example that seems to plot on the edges instead of the nodes.

这篇关于如何在 NetworkX 中的节点上显示图像图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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