简体   繁体   English

使用Python中的networkX用表情符号替换节点标签

[英]Substitute node labels with emoji using networkX in Python

I am using networkX to draw a network plot from a distance matrix(emoji_sim, a DataFrame). 我正在使用networkX从距离矩阵(emoji_sim,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))))   

But I want to substitute the nodes label with images(possibly read from files or using Python Emoji package). 但我想用节点标签替换图像(可能从文件中读取或使用Python表情符号包)。 Is there any way to do that? 有没有办法做到这一点? Thanks a lot! 非常感谢!

To clarify, I am trying to substitute the actual circle with images. 为了澄清,我试图用图像替换实际的圆圈。

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. 它背后的想法不是很难,但为了让它显示(至​​少在ubunto上)它给了我一些困难,因为并非所有字体都支持表情符号。 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. 从emoji python包中表情符号备忘单中 ,我选择了三个作为示例显示,这里是代码。

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()

在此输入图像描述

Difficulties encountered: 遇到的困难:

1- My machine is on ubunto 14.04, I could not display any emoji they always appeared as rectangles 1-我的机器在ubunto 14.04上,我无法显示任何表情符号,它们总是显示为矩形

Installed needed font Symbola using the following command (mentioned here) : 使用以下命令安装所需的字体Symbola (此处提到)

sudo apt-get install ttf-ancient-fonts

2- Maplotlib (which networkx calls to draw) is not using the installed font. 2- Maplotlib(networkx调用绘图)未使用已安装的字体。

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). 从几个有用的讨论1 2 3 4 5 6我在默认的matplotib目录中复制并粘贴了Symbola的.tff字体文件(它获取要使用的字体)。

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

Then I had to delete fontList.cache file for the new font to be loaded. 然后我不得不删除fontList.cache文件以获取要加载的新字体。

rm ~/.cache/matplotlib/fontList.cache

Note 注意

You can have different views by changing the input to the draw_networkx eg 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 . 您可以通过更改draw_networkx的输入来拥有不同的视图,例如,不发送linewidths将为每个节点显示圆形边框,如果您想要节点的特定背景颜色,请将color_node从白色更改为您想要的颜色...更多细节请查看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM