Skip to content

Instantly share code, notes, and snippets.

@yamakk
Created September 28, 2011 07:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yamakk/1247256 to your computer and use it in GitHub Desktop.
Save yamakk/1247256 to your computer and use it in GitHub Desktop.
patch for enables function draw_networkx_edge_labels to set fontproperties.
--- 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,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment