Available "font-family" entry for draw_networkx_labels

I have this code:

    labels_params = {"labels":{n: "" if n[0] == "." else n for n in G.nodes () },
                     "font_family":"sans-serif",
                     "alpha":.5,
                     "font_size":16 }

    draw_networkx_labels (G, pos, **labels_params)

My problem is that I want to be able to display more than two fonts on my output chart.

I want to know how I can detect all possible entries for "font_family".

I looked at the FontManager code from Networkx and I only see "sans-serif".

I work in X11 under Ubuntu.

+4
source share
1 answer

draw_networkx_labels, ax.text ( ax - matplotlib). , , , MPL- (docs).

, ; , (, "serif" ) . http://www.w3schools.com/css/css_font.asp

, , , , .

, :

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

() . , , demo .

:

[i for i in matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf') if 'times' in i.lower()]

avail_font_names ; , .

import matplotlib.pyplot as plt
import networkx as nx

font_names = ['Sawasdee', 'Gentium Book Basic', 'FreeMono', ]
family_names = ['sans-serif', 'serif', 'fantasy', 'monospace']


# Make a graph
G  = nx.generators.florentine_families_graph()

# need some positions for the nodes, so lay it out
pos = nx.spring_layout(G)

# create some maps for some subgraphs (not elegant way)
subgraph_members  = [G.nodes()[i:i+3] for i in xrange(0, len(G.nodes()), 3)]

plt.figure(1)
nx.draw_networkx_nodes(G, pos)


for i, nodes in enumerate(subgraph_members):
    f = font_names[(i % 3)]
    #f = family_names[(i % 4)]
    # extract the subgraph
    g = G.subgraph(subgraph_members[i])
    # draw on the labels with different fonts
    nx.draw_networkx_labels(g, pos, font_family=f, font_size=40)

# show the edges too
nx.draw_networkx_edges(G, pos)


plt.show()    

example figure showing different font labelling

" ..."

: , "UserWarning: findfont: [sans-serif]] . ...", , , nabble : (yup, , .)

rm ~/.matplotlib/fontList.cache 
+5

All Articles