= I made a graph using networkx of python, so make a note
make_graph.py
# -*- encoding:utf-8 -*-
import networkx
import pylab
from matplotlib import font_manager
from itertools import combinations
from random import randint
#A dict whose key is a node and the list of nodes that have an edge is a value.
vector = {}
persons = [u"Tanaka", u"Suzuki", u"Yamada", u"Kimura", u"Yoshioka"]
edge_labels = {}
for person in persons:
# defaultdict(list)Do this to create a node instead
vector[person] = []
for man_pair in combinations(persons, 2):
man1, man2 = man_pair
#Price the edges appropriately
r = randint(1, 10)
if r % 2:
continue
else:
vector[man1].append(man2)
edge_labels[(man1, man2)] = r
graph = networkx.Graph(vector) #Undirected graph
# graph = network.DiGraph(vector) #Directed graph(to_Can be converted to an undirected graph with undirected)
pylab.figure(figsize=(3, 4)) #Make it 3 inches wide and 4 inches long
pos = networkx.spring_layout(graph) #Plot nicely
# pos = networkx.random_layout(graph)Anyway, you can plot at high speed
#Change font (font_change path as appropriate)
font_path = "/usr/share/fonts/japanese/TrueType/sazanami-gothic.ttf"
font_prop = font_manager.FontProperties(fname=font_path)
networkx.set_fontproperties(font_prop)
#Tweak the look
networkx.draw_networkx_nodes(graph, pos, node_size=100, node_color="w")
networkx.draw_networkx_edges(graph, pos, width=1)
networkx.draw_networkx_edge_labels(graph, pos, edge_labels=edge_labels)
networkx.draw_networkx_labels(graph, pos, font_size=16, font_color="r")
pylab.xticks([])
pylab.yticks([])
pylab.show()
pylab.savefig("graph_networkx.png ")
Here, Japanese cannot be displayed unless the version of networkx is 1.5 or higher. Probably
ValueError: matplotlib display text must have all code points < 128 or use Unicode strings
I think you will get the error. Please apply the patch here to networkx / drawing / nx_pylab.py to display Japanese.
Like this
*** Reference http://d.hatena.ne.jp/nishiohirokazu/20111121/1321849806 http://antibayesian.hateblo.jp/entry/20110828/1314491180
Recommended Posts