Logo Questions Linux Laravel Mysql Ubuntu Git Menu

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_size":16 }

    draw_networkx_labels (G, pos, **labels_params)

My problem is that I wish to be able to display in my output graph more than 2 fonts .

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

I browsed the code of FontManager from Networkx, and I see only "sans-serif".

I am working in X11 under Ubuntu.

like image 496
alinsoar Avatar asked Mar 21 '23 12:03


1 Answers

Poking around a bit in the draw_networkx_labels source, it does indeed come down to a call to ax.text (where ax is a matplotlib axis). So that means you should have as much configurability as you get with any normal MPL text (docs).

Font names vs font families

As far as I can tell, a font name is an instance of a font family; though because the generic families (eg 'serif') are often given as settings to font family variables. http://www.w3schools.com/css/css_font.asp

This has confused me for a while, so please correct me if I'm wrong here.

Finding the full list of fonts

So if you use the technique from here:

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

you get all the (specific) options. Don't know where you find a fuller list than the font demo for generics.

this post shows a method for searching by name:

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

Using more than one font for labels in your graph

From the avail_font_names list above, I picked out three for this example; you might have to substitute some of them depending on what you have installed.

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)]

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)


example figure showing different font labelling

Resolving the 'Falling back to ...' warnings

Note: if you get errors when of the form "UserWarning: findfont: Font family ['sans-serif'] not found. Falling back to ...", when trying to use fonts even if they do exist, this nabble dialog suggests clearing the font cache: (yup, this will irretrievably remove a file but it is auto-generated.)

rm ~/.matplotlib/fontList.cache 
like image 151
Bonlenfum Avatar answered Mar 24 '23 03:03