简体   繁体   English

networkx draw_graphviz中文标签

[英]networkx draw_graphviz with chinese label

I have a graph where the node labels are in Chinese. 我有一个图表,其中节点标签是中文的。 I want to draw it using draw_graphviz() , but when saving the image, no Chinese characters will display. 我想使用draw_graphviz()绘制它,但在保存图像时,不会显示任何中文字符。 They instead show as white blocks. 它们显示为白色块。

I just want to know how to set the font. 我只是想知道如何设置字体。

nx.draw_graphviz(G, font_size=6, node_size=80, font_family='serif', font_color='b', alpha=0.1)       
plt.savefig("community__large" + str(i) + ".png")  
plt.close()

The above is the code I am using now. 以上是我现在使用的代码。

...where all Chinese word not display instead with block white. ...所有中文单词都不显示而是用白色块显示。

At first I couldn't replicate the behaviour you describe when using plt.show() , but when saving the image, the characters show up as white blocks. 起初,我无法复制您在使用plt.show()时所描述的行为,但在保存图像时,字符显示为白色块。 On my system (OS X 10.8.2, Python 2.7.3), the font SimHei seems to resolve the issue. 在我的系统(OS X 10.8.2,Python 2.7.3)上,SimHei字体似乎解决了这个问题。 From the matplotlib mailing list I found that the font Microsoft Yahei could work as well, but I haven't tried. matplotlib邮件列表中我发现Microsoft Yahei的字体也可以正常工作,但我还没试过。

g = nx.Graph()

g.add_edge('挪威'.decode('utf8'), '瑞典'.decode('utf8'))
nx.draw_graphviz(g, font_family='SimHei', node_size=1000,
    node_color='white')
plt.savefig('plot.png')

This produced the following image: 这产生了以下图像:

带有中文标签的图表

Note that I got the Chinese characters from Google Translate, so I hope it's not anything offensive. 请注意,我从谷歌翻译中获得了汉字,所以我希望这不是冒犯性的。

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

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