我试图将matplotlib的默认字体更改为Helvetica Neue。一段时间前,在我和环保署/Canopy的Mac上,一切都很好。
现在尝试着在ubuntu上做同样的事情,但这是行不通的。
我就是这样做的:
但这些步骤对我都没有用。
$ python -c 'from matplotlib import pyplot as plt; plt.plot(1); plt.savefig("/tmp/test.png")'
/usr/local/lib/python2.7/dist-packages/matplotlib-1.3.0-py2.7-linux-x86_64.egg/matplotlib/font_manager.py:1236:
UserWarning: findfont: Font family ['Helvetica Neue'] not found. Falling back to Bitstream Vera Sans
(prop.get_family(),self.defaultFamilyfontext)
版本为1.3.0
$ python -c 'import matplotlib; print matplotlib.__version__'
1.3.0
我还试着把字体移到~/.config/matplotlib/fonts/ttf
上,但没有成功。
编辑:,正如我建议的那样,我尝试为特定文本选择特定字体。
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
path = '/home/<myusername>/.fonts/HelveticaNeue.ttf'
prop = font_manager.FontProperties(fname=path)
prop.set_weight = 'light'
mpl.rcParams['font.family'] = prop.get_name()
mpl.rcParams['font.weight'] = 'light'
fig, ax = plt.subplots()
ax.set_title('Text in a cool font', fontproperties=prop, size=40)
plt.savefig('/tmp/test2.png')
但这没什么区别。
/usr/local/lib/python2.7/dist-packages/matplotlib-1.3.0-py2.7-linux-x86_64.egg/matplotlib/font_manager.py:1236:
UserWarning: findfont: Font family ['Helvetica Neue'] not found. Falling back to Bitstream Vera Sans
但是,我似乎只有在Helvetica/Helvetica新字体中才会遇到这个问题。(prop.get_family(),self.defaultFamilyfontext)
发布于 2014-02-14 10:45:39
这不会永久改变你的字体,但值得一试。
matplotlib.rc('font', family='sans-serif')
matplotlib.rc('font', serif='Helvetica Neue')
matplotlib.rc('text', usetex='false')
matplotlib.rcParams.update({'font.size': 22})
发布于 2015-02-07 12:38:48
Ubuntu 14.04 LTS
上传字体
sudo cp NotoSansKR-Regular.otf /usr/share/fonts/
更新字体缓存
sudo fc-cache -fv
您可以检查字体列表。
fc-list
重新启动ipython等。检查字体列表。
[f.name for f in matplotlib.font_manager.fontManager.ttflist]
取你的字号
import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['font.family'] = 'Noto Sans Korean'
画
plt.title(u'한글 제목')
plt.xlabel(u'한글 축 이름')
plt.plot(range(5))
发布于 2015-11-03 07:59:06
Kim已经引入了动态解决方案,这是非常完美的,这里还有另外两种在静态中实现相同的方法。
首先,您可以为matplotlib在rc文件中放置一行。有关定位文件和详细设置的详细信息,请参阅此页。
font.family : NanumGothic
其次,如果您正在使用ipython,您可以将一些字体设置命令放置到交互式shell的配置文件中。查找名为ipython_config.py的文件,该文件通常位于~/..ipython/下面。然后向列表中再添加两行,c.InteractiveShellApp.exec_lines。
c.InteractiveShellApp.exec_lines = [
"import matplotlib as mpl",
"mpl.rcParams['font.family'] = 'NanumGothic'"
]
当脚本导入matplotlib时,当shell脚本加载字体时,它总是工作在任何环境中。
https://stackoverflow.com/questions/21461155
复制相似问题