可用的“字体家族" draw_networkx_labels的条目 [英] Available "font-family" entry for draw_networkx_labels

查看:581
本文介绍了可用的“字体家族" draw_networkx_labels的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

    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)

我的问题是我希望能够在输出图中显示2种以上的字体.

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

我想知道如何检测"font_family"的所有可能条目.

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

我从Networkx浏览了FontManager的代码,但仅看到"sans-serif".​​

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

我正在Ubuntu下的X11中工作.

I am working in X11 under Ubuntu.

推荐答案

draw_networkx_labels源代码中仔细查看,确实可以归结为对ax.text的调用(其中ax是matplotlib轴).因此,这意味着您应该具有与任何普通MPL文本一样的可配置性(

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

据我所知,字体名称是字体家族的一个实例.虽然因为 通常将通用系列(例如"serif")作为字体系列的设置 变量. http://www.w3schools.com/css/css_font.asp

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.

这篇文章显示了搜索的方法按名称:

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

为图形中的标签使用多种字体

在上面的avail_font_names列表中,我为该示例选择了三个.您可能必须根据安装的内容来替换其中一些.

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

注意:如果在形式为"UserWarning:findfont:找不到字体家族['sans-serif'].回退到..."时出错,则尝试使用即使存在的字体也是如此 nabble对话框建议清除字体缓存:(是的,这将无法删除地删除文件,但它是自动生成的.)

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 

这篇关于可用的“字体家族" draw_networkx_labels的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆