Skip to content

Instantly share code, notes, and snippets.

@koorukuroo
Forked from yamakk/nx_pylab.py.patch
Created July 18, 2014 12:07
Show Gist options
  • Save koorukuroo/3aae9a3e900e868840ea to your computer and use it in GitHub Desktop.
Save koorukuroo/3aae9a3e900e868840ea to your computer and use it in GitHub Desktop.
--- networkx-1.5/networkx/drawing/nx_pylab.py 2011-06-04 09:45:38.000000000 +0900
+++ nx_pylab.py 2011-10-01 01:48:06.000000000 +0900
@@ -21,7 +21,8 @@
# All rights reserved.
# BSD license.
-__all__ = ['draw',
+__all__ = ['set_fontproperties',
+ 'draw',
'draw_networkx',
'draw_networkx_nodes',
'draw_networkx_edges',
@@ -38,6 +39,13 @@
import networkx as nx
from networkx.drawing.layout import shell_layout,\
circular_layout,spectral_layout,spring_layout,random_layout
+import matplotlib.font_manager
+
+font_path = matplotlib.font_manager.fontManager.defaultFont['ttf']
+fontproperties = matplotlib.font_manager.FontProperties(fname=font_path)
+def set_fontproperties(font):
+ global fontproperties
+ fontproperties = font
def draw(G, pos=None, ax=None, hold=None, **kwds):
"""Draw the graph G with Matplotlib (pylab).
@@ -718,9 +726,10 @@
if not cb.is_string_like(label):
label=str(label) # this will cause "1" and 1 to be labeled the same
t=ax.text(x, y,
- label,
+ unicode(label),
size=font_size,
color=font_color,
+ fontproperties=fontproperties,
family=font_family,
weight=font_weight,
horizontalalignment=horizontalalignment,
@@ -849,9 +858,10 @@
verticalalignment=kwds.get('verticalalignment','center')
t=ax.text(x, y,
- label,
+ unicode(label),
size=font_size,
color=font_color,
+ fontproperties=fontproperties,
family=font_family,
weight=font_weight,
horizontalalignment=horizontalalignment,
@koorukuroo
Copy link
Author

terms = [t.decode('utf8') for t in terms]

nx.draw_networkx(g)
pos = nx.spectral_layout(g)
nx.draw_networkx_nodes(g, pos, node_size=100, node_color="w")
nx.draw_networkx_edges(g, pos, width=1)
nx.draw_networkx_edge_labels(g, pos, edge_labels=edge_labels)
nx.draw_networkx_labels(g, pos, font_size=16, font_color="r")
nx.draw_networkx_labels(g, pos, font_family='NanumGothicOTF')
굳이 버그리포팅이 되지 않았던 이유는 unicode로 써야 하기 때문.

avail_font_names = [f.name for f in matplotlib.font_manager.fontManager.ttflist]

@koorukuroo
Copy link
Author

사용방법
import matplotlib.font_manager as fm
fp1 = fm.FontProperties(fname="./NotoSansKR-Regular.otf") # 무료 폰트 https://www.google.com/get/noto/pkgs/NotoSansKorean-windows.zip
nx.set_fontproperties(fp1)
G = nx.Graph()

@koorukuroo
Copy link
Author

http://pythonkr.github.io/pyconkr-2014/pdf/pyconkr-2014-07_networkx.pdf

import matplotlib.font_manager as fm
fp1 = fm.FontProperties(fname="./NanumGothic.otf")
nx.set_fontproperties(fp1)
G = nx.Graph()
G.add_edge(u'한국어',u'영어')
nx.draw(G, with_labels=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment