![](/img/trans.png)
[英]ipython notebook: problems with matplotlib rc-file and custom css
[英]custom matplotlib font in ipython notebook
我在iPython笔记本中使用matplotlib,希望将字体系列更改为Gotham-Book。 我在计算机上安装了Gotham-Book,但不幸的是,matplotlib似乎找不到该字体,并且默认使用衬线字体。 关于如何使哥谭书生效的任何想法?
这是我正在使用的代码。
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)
我可以在这里看到字体在我的机器上:
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']
我相信,因为即使你添加的font.family到rcParams,但你还没有使用它的xlabel:
mpl.pylab.xlabel("GOTHAM BOOK FONT", size=20)
更改为此,应该工作:
mpl.pylab.xlabel("GOTHAM BOOK FONT", family='gothambook', size=20)
您可以使用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)
结果:
或者,您可以通过以下方式即时设置参数:
plt.xlabel('Comic Sans MS FONT', family='Comic Sans MS', fontsize=18, color='blue')
会有相同的结果,您可以阅读有关“ 文本属性 ”中参数的更多信息
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.