对“matplotlib”字体设置的奇怪反应 [英] Strange response to font settings in 'matplotlib'

查看:342
本文介绍了对“matplotlib”字体设置的奇怪反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的系统(OS X 10.9.1)上安装了各种字体,并且能够在我的所有工具和应用程序(包括我的LaTeX安装)中使用它们,但是我尝试指定 matplotlib (带有 text.usetex = False )会产生意想不到的结果。



例如

  font = {'family':'sans-serif',
'sans-serif':['Open Sans','Avenir','sans-serif']}
plt.rc('font',** font)

结果既不使用Open Sans,也不使用Avenir,尽管指定了一个大小(例如,'size'
$ b 奇怪的是,指定一个重量不仅会改变重量,还会导致字体改变。例如

  font = {'family':'sans-serif',
'sans-serif':[
'weight':'bold'}
plt.rc('font',** font)
code>

似乎导致Open Sans被使用(但是看起来像是超粗体而不是粗体)。如果我减重550或更多,我会得到相同的效果;而anythng少就会导致错误的字体。与其他字体(例如Sika或Gill Sans)类似的奇怪行为,不管字体是OT还是TT,或者它是否显示为系统字体或用户字体,都可以看到这个字体。



为什么(仅) matplotlib 以这种奇怪的方式响应。有没有 matplotlib (或Python)设置或配置选项我应该改变,以确保我得到预期的行为?有没有其他的方法,我应该指定字体的重量,也许?






FWIW,OpenSans如何看待我字体安装:





所有这些都是Python预期的(在 / Users / Rax / Library / Fonts / 中) p>

  import matplotlib.font_manager 
print matplotlib.font_manager.findSystemFonts(fontpaths = None)



  font_manager.FontProperties(fname ='/ Users / Rax / Library / Fonts / OpenSans-ExtraBold.ttf').get_name()
font_manager.FontProperties(fname ='/ Users / Rax / Library / Fonts / OpenSans-Regular.ttf')。 get_name()

如预期的那样报告Open Sans作为字体名称,

  font_manager.FontProperties(fname ='/ Users / Rax / Library / Fonts / OpenSans-Regular.ttf')。get_weight()

报告'正常'。

解决方案 div>

如果你还没有,你可能需要建立一个 matplotlibrc 文件并查看这些设置是否保留。把它放在〜/ .matplotlib / matplotlibrc 中,它会在启动时自动读取。我还没有为我的字体定制,但是当我修改了一些其他的设置(特别是图像DPI)时,我用pylab重新启动IPython,所以希望它能适合你。

b $ b

I have a variety of fonts installed on my system (OS X 10.9.1), and am able to use them all with no problems in all of my tools and applications (including my LaTeX installation) but my attempts to specify a specific font in matplotlib (with text.usetex=False) produce unexpected and confusing results.

For example

font = {'family': 'sans-serif', 
        'sans-serif': ['Open Sans', 'Avenir', 'sans-serif']}
plt.rc('font', **font)

results in neither Open Sans nor Avenir being used, though specifying a size (e.g., 'size': 20) changes the size as expected.

Strangely, specifying a weight not only changes the weight, but can also result in the font changing. For example

font = {'family': 'sans-serif', 
        'sans-serif': ['Open Sans', 'Avenir', 'sans-serif'],
        'weight': 'bold'}
plt.rc('font', **font)

seems to result in Open Sans being used (but as something that looks like extra-bold rather than bold). If I make weight 550 or more I get the same effect; while anythng less results in the wrong font. I get similar odd behavior with other fonts (e.g.. Sika or Gill Sans), and see this regardless of of whether the font is OT, TT, or whether it is indicated as a System or User font.

Why is (only) matplotlib responding in this strange way. Are there matplotlib (or Python) settings or configuration options I should be changing to ensure that I get the expected behavior? Is there some other way I should be specifying the font weight, perhaps?


FWIW, here's how Open Sans looks in my font installation:

All of these are found by Python as expected (in /Users/Rax/Library/Fonts/) by

import matplotlib.font_manager
print matplotlib.font_manager.findSystemFonts(fontpaths=None)

and

font_manager.FontProperties(fname='/Users/Rax/Library/Fonts/OpenSans-ExtraBold.ttf').get_name()
font_manager.FontProperties(fname='/Users/Rax/Library/Fonts/OpenSans-Regular.ttf').get_name()

both report 'Open Sans' as the font name, as expected, with

font_manager.FontProperties(fname='/Users/Rax/Library/Fonts/OpenSans-Regular.ttf').get_weight()

reporting 'normal'.

解决方案

If you don't have one already, you might want to set up a matplotlibrc file and see if those settings stick. Put it in ~/.matplotlib/matplotlibrc and it'll be read automatically upon startup. I haven't customized mine for fonts yet, but when I altered some other settings in it (image DPI, specifically) it was picked up when I restarted IPython with pylab, so hopefully it'll work for you.

这篇关于对“matplotlib”字体设置的奇怪反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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