简体   繁体   English

ipython Notebook中的自定义matplotlib字体

[英]custom matplotlib font in ipython notebook

I use matplotlib in iPython notebooks and I'm hoping to change the font-family to Gotham-Book. 我在iPython笔记本中使用matplotlib,希望将字体系列更改为Gotham-Book。 I have Gotham-Book installed on my machine but unfortunately, matplotlib cannot seem to find the font and keeps defaulting to a serif font. 我在计算机上安装了Gotham-Book,但不幸的是,matplotlib似乎找不到该字体,并且默认使用衬线字体。 Any thoughts on how to get gotham-book working? 关于如何使哥谭书生效的任何想法?

Here is the code I'm using. 这是我正在使用的代码。

import matplotlib.pylab as plt

plt.rcParams['font.family']=['gothambook','gotham','gotham-book','serif']

mpl.pylab.plot(range(10), mpl.pylab.sin(range(10)))
mpl.pylab.xlabel("GOTHAM BOOK FONT", size=20)

在此处输入图片说明

I can see the font is on my machine here: 我可以在这里看到字体在我的机器上:

In [12]:
matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')

Out[12]:
['/usr/share/fonts/dejavu/DejaVuLGCSansMono-Oblique.ttf',
 '/usr/share/fonts/dejavu/DejaVuLGCSansMono.ttf',
 '/usr/share/fonts/dejavu/DejaVuSansMono-BoldOblique.ttf',
 '/usr/share/fonts/dejavu/DejaVuSans-ExtraLight.ttf',
 '/usr/share/fonts/dejavu/DejaVuLGCSansMono-BoldOblique.ttf',
 '/usr/share/fonts/dejavu/DejaVuSansCondensed-Oblique.ttf',
 '/usr/share/fonts/dejavu/DejaVuSans.ttf',
 '/usr/local/share/fonts/gothambook/Gotham-Book.ttf',
 '/usr/share/fonts/dejavu/DejaVuSansCondensed.ttf',
 '/usr/share/fonts/dejavu/DejaVuSansMono-Oblique.ttf',
 '/usr/share/fonts/dejavu/DejaVuLGCSansMono-Bold.ttf',
 '/usr/share/fonts/dejavu/DejaVuSans-BoldOblique.ttf',
 '/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf',
 '/usr/share/fonts/dejavu/DejaVuSansCondensed-BoldOblique.ttf',
 '/usr/share/fonts/dejavu/DejaVuSansMono-Bold.ttf',
 '/usr/share/fonts/dejavu/DejaVuSansCondensed-Bold.ttf',
 '/usr/share/fonts/dejavu/DejaVuSans-Oblique.ttf',
 '/usr/share/fonts/dejavu/DejaVuSansMono.ttf']

I believe because even you ADDED the font.family to the rcParams but you haven't USED it on the xlabel : 我相信,因为即使你添加font.familyrcParams,但你还没有使用它的xlabel:

mpl.pylab.xlabel("GOTHAM BOOK FONT", size=20)

Change it to this, should work: 更改为此,应该工作:

mpl.pylab.xlabel("GOTHAM BOOK FONT", family='gothambook', size=20)

You can use fontdict to change the font settings on the xlabel , something like this works on mine: 您可以使用fontdict改变对xlabel字体设置,像这样的作品对我的:

import matplotlib
import matplotlib.pyplot as plt

plt.rcParams['font.family']
Out[1]: [u'sans-serif']

plt.rcParams['font.family'].append(u'Comic Sans MS')

font = {
    'family' : 'Comic Sans MS',
    'color'  : 'blue',
    'weight' : 'normal',
    'size'   : 18,
}

plt.plot(range(10), matplotlib.pylab.sin(range(10)))
plt.xlabel('Comic Sans MS FONT', fontdict=font)

Results: 结果:

Matplotlib使用不同的字体

Alternatively you can set parameters on the fly with this: 或者,您可以通过以下方式即时设置参数:

plt.xlabel('Comic Sans MS FONT', family='Comic Sans MS', fontsize=18, color='blue')

which will have same results, you can read more about the parameters on Text Properties 会有相同的结果,您可以阅读有关“ 文本属性 ”中参数的更多信息

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM