在Python中使用networkX用表情符号替换节点标签 [英] Substitute node labels with emoji using networkX in Python

查看:408
本文介绍了在Python中使用networkX用表情符号替换节点标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用networkX从距离矩阵(emoji_sim,DataFrame)绘制网络图.这是代码:

I am using networkX to draw a network plot from a distance matrix(emoji_sim, a DataFrame). Here is the code:

G = nx.from_numpy_matrix(np.array(emoji_sim))
nx.draw(G, edge_color='silver', node_color='lightsalmon', with_labels=True)
plt.show()

我知道有一种方法可以将节点重新标记为:

I know there is a way to relabel the nodes as:

G = nx.relabel_nodes(G, dict(zip(range(len(G.nodes())), range(1, len(G.nodes())+1))))   

但是我想用图像替换节点标签(可能从文件中读取或使用Python Emoji包读取).有什么办法吗?非常感谢!

But I want to substitute the nodes label with images(possibly read from files or using Python Emoji package). Is there any way to do that? Thanks a lot!

为澄清起见,我正在尝试用图片替换实际的圆圈.

To clarify, I am trying to substitute the actual circle with images.

推荐答案

它背后的想法不是很困难,但是为了使其能够显示(至少在ubunto上),它给了我一些困难,因为不是所有字体支持表情符号.我将展示简单明了的方法,然后显示一些最终对我有帮助的链接(也许您将不需要这些链接).

The idea behind it is not very difficult but in order to get it to be displayed (at least on ubunto) it gave me some hard time as not all fonts support emoji. I shall display the straight forward way then some links that helped me in the end (maybe you will not need those).

来自表情符号速查表,来自

From emoji cheat sheet from the emoji python package I picked up three to be shown as an example and here is the code.

G = nx.Graph()
G.add_nodes_from([0,1,2])
n0 = emoji.emojize(':thumbsup:',use_aliases=True)
n1 = emoji.emojize(':sob:',use_aliases=True)
n2 = emoji.emojize(':joy:',use_aliases=True)
labels ={0:n0,1:n1,2:n2}
nx.draw_networkx(G,labels=labels, node_color = 'w', linewidths=0, with_labels=True, font_family = 'Symbola' ,font_size = 35)    
plt.show()

遇到的困难:

1-我的机器在ubunto 14.04上,我无法显示它们始终显示为矩形的任何表情符号

1- My machine is on ubunto 14.04, I could not display any emoji they always appeared as rectangles

使用以下命令

Installed needed font Symbola using the following command (mentioned here):

sudo apt-get install ttf-ancient-fonts

2- Maplotlib(networkx调用其绘制)未使用安装的字体.

2- Maplotlib (which networkx calls to draw) is not using the installed font.

从一些有用的讨论中 1 3 4 5

From several useful discussions 1 2 3 4 5 6 I copied and pasted the .tff font file of Symbola in the default matplotib directory (where it fetches for fonts to use).

cp /usr/share/fonts/truetype/ttf-ancient-scripts/Symbola605.ttf /usr/share/matplotlib/mpl-data/fonts/ttf

然后我必须删除fontList.cache文件才能加载新字体.

Then I had to delete fontList.cache file for the new font to be loaded.

rm ~/.cache/matplotlib/fontList.cache

注意

您可以通过更改draw_networkx的输入来获得不同的视图,例如不发送linewidths将为每个节点显示圆形边框,同样,如果您想要节点的特定背景色,请将color_node从白色更改为所需的颜色...有关更多详细信息,请检查

You can have different views by changing the input to the draw_networkx e.g. not sending the linewidths will show circular border for each node, also if you want a specific background color for nodes change the color_node from white to a color that you want ... for more details check the documentation.

这篇关于在Python中使用networkX用表情符号替换节点标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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