Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

python - Change Matplotlib's default font

I'm trying to change matplotlib's default font to Helvetica Neue. On my Mac with EPD/Canopy everything worked fine some time ago.

Trying to do the same on ubuntu now and it's not working.

This is what I did:

  1. Installed Helvetica Neue

    $ fc-match 'Helvetica Neue':Light
    HelveticaNeue-Light.otf: "Helvetica Neue" "細體"
    
  2. Converted the odt/dfont into ttf:

    fondu -show HelveticaNeue.dfont
    
  3. changed matplotlibrc to

    $ cat ~/.config/matplotlib/matplotlibrc
    ...
    font.family: Helvetica Neue
    

    I also tried with:

    font.family: sans-serif
    font.sans-serif: Helvetica Neue
    
  4. I removed the font cache

    rm ~/.config/matplotlib/fontList.cache
    

But none of these steps are working for me.

    $ 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.defaultFamily[fontext]))

Version is 1.3.0

    $ python -c 'import matplotlib; print  matplotlib.__version__'
    1.3.0

I also tried moving the fonts to ~/.config/matplotlib/fonts/ttf but it didn't work.


EDIT: As suggested I tried selecting a specific font for a specific text.

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')

But it makes no difference.

/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

However I seem to experience this problem only with this Helvetica/Helvetica Neue font. (prop.get_family(), self.defaultFamily[fontext]))

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This will not change your font permanently, but it's worth a try.

matplotlib.rc('font', family='sans-serif') 
matplotlib.rc('font', serif='Helvetica Neue') 
matplotlib.rc('text', usetex='false') 
matplotlib.rcParams.update({'font.size': 22})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...