jeffery
jeffery

Reputation: 31

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. 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.

Upvotes: 3

Views: 2523

Answers (1)

Maehler
Maehler

Reputation: 6331

...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. On my system (OS X 10.8.2, Python 2.7.3), the font SimHei seems to resolve the issue. From the matplotlib mailing list I found that the font Microsoft Yahei could work as well, but I haven't tried.

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:

Graph with Chinese labels

Note that I got the Chinese characters from Google Translate, so I hope it's not anything offensive.

Upvotes: 5

Related Questions