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.
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).
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.
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()]
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()
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With