这听起来像一个简单的问题,但我没有找到任何有效的解决方案来改变python中使用matplotlib制作的图中的字体(而不是字体大小).
我发现了几个教程,通过修改matplotlib存储其默认字体的文件夹中的一些文件来更改matplotlib的默认字体 - 请参阅此博客文章 - 但我正在寻找一个不太激进的解决方案,因为我想使用多个我的情节中的字体(文字,标签,轴标签等).
我正在尝试将matplotlib的默认字体更改为Helvetica Neue.在EPD/Canopy的Mac上,一切都很好.
现在尝试在ubuntu上做同样的事情并且它不起作用.
这就是我做的:
已安装Helvetica Neue
$ fc-match 'Helvetica Neue':Light
HelveticaNeue-Light.otf: "Helvetica Neue" "??"
Run Code Online (Sandbox Code Playgroud)将odt/dfont转换为ttf:
fondu -show HelveticaNeue.dfont
Run Code Online (Sandbox Code Playgroud)将matplotlibrc更改为
$ cat ~/.config/matplotlib/matplotlibrc
...
font.family: Helvetica Neue
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
font.family: sans-serif
font.sans-serif: Helvetica Neue
Run Code Online (Sandbox Code Playgroud)我删除了字体缓存
rm ~/.config/matplotlib/fontList.cache
Run Code Online (Sandbox Code Playgroud)但这些步骤都不适合我.
$ 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
Run Code Online (Sandbox Code Playgroud)
(prop.get_family(),self.defaultFamily [fontext]))
版本是1.3.0
$ python -c 'import matplotlib; print matplotlib.__version__'
1.3.0
Run Code Online (Sandbox Code Playgroud)
我也尝试过移动字体,~/.config/matplotlib/fonts/ttf但它没有用.
编辑: 建议我尝试为特定文本选择特定字体.
import matplotlib …Run Code Online (Sandbox Code Playgroud) 所以我已经尝试了几乎所有我能在 stackoverflow 上找到的东西(以及谷歌会引导我的其他任何地方);我就是不能改变该死的字体!
这是我迄今为止尝试过的非详尽清单:
按照这个问题中的建议尝试:
import matplotlib.pyplot as plt
csfont = {'fontname':'Times New Roman'}
x = [1,2,3]
y = x
plt.plot(x,y)
plt.title('Please be Times >__<',**csfont)
plt.show()
Run Code Online (Sandbox Code Playgroud)
给我这个错误日志:
>>> (executing file "<tmp 1>")
Note on using QApplication.exec_():
The GUI event loop is already running in the pyzo kernel, and exec_()
does not block. In most cases your app should run fine without the need
for modifications. For clarity, this is what the pyzo kernel does:
- Prevent deletion of …Run Code Online (Sandbox Code Playgroud)