Available "font-family" entry for draw_networkx_labels
Asked Answered
B

1

4

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 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.

Bluefish answered 25/11, 2013 at 1:26 Comment(3)
As I understand it, networkx uses matplotlib for most of its drawing. Does this post help identify what fonts you have available on your system? Matplotlib typically has several families available -- as illustrated in this demoDisconnected
Thank you for your reply. No, it does not help :( . This is the man page of draw_labels: networkx.github.io/documentation/latest/reference/generated/… -- It does not have any parameter "fontname", but only fontfamily. My question is about setting node labels, not about setting the title ... :(Bluefish
matplotlib.org/examples/pylab_examples/fonts_demo.html <= This page is closer of what I want. It provides a list of fontfamilies accepted by draw_labels. My question is what it the complete list, and how can I see in the system what are the available families...Bluefish
D
7

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

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

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 
Disconnected answered 25/11, 2013 at 20:47 Comment(3)
Any comment is too much ! Thank you ! Thank you !Bluefish
You happened to ask on the day where I also needed to resolve a very similar font problem ;) Glad it is of use.Disconnected
how can I get an arabic fonts working? which font family should I use?Iyar

© 2022 - 2024 — McMap. All rights reserved.