33

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

john-hen
  • 4,410
  • 2
  • 23
  • 40
gozzilli
  • 8,089
  • 11
  • 56
  • 87
  • 1
    Before trying to change the default, have you been able to use the new font directly/explicitly? http://stackoverflow.com/a/18821968/1643946 shows one way to list the fonts that python/mpl has available, and you could try using `plt.text(1, 1, 'rattling', font_family="Helvetica Neue")`. See also http://stackoverflow.com/a/16574948/1643946 – Bonlenfum Jan 30 '14 at 18:28
  • Fair point. I've tried now with no success. Edited the question accordingly. – gozzilli Jan 30 '14 at 19:39
  • Can you try this installing from the master branch? There has been a bunch of work recently related to font-lookup. It also looks like the font has non-ascii in the name, iirc there is a bug someplace upstream that makes that not work. – tacaswell Jan 31 '14 at 03:25
  • @tcaswell Tried with master ('1.4.x') and it's the same. Note that the same font (literally the same, I copied it across) works fine on my MacOS machine. – gozzilli Jan 31 '14 at 20:03
  • 1
    Please create an issue on github for this. It smells like a bug to me. – tacaswell Jan 31 '14 at 20:30
  • Maybe you also have to delete `fontList.cache`? That's what I had to do. – mcmayer Nov 27 '17 at 09:28

4 Answers4

18

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})
vitaliis
  • 4,082
  • 5
  • 18
  • 40
Brian
  • 13,996
  • 19
  • 70
  • 94
  • 1
    I think the problem is not with the default behaviour, but more with the fact that it's not recognising my Helvetica Neue font. I filed a bug on github. – gozzilli Feb 14 '14 at 14:43
  • I'm using the EPD python version (https://www.enthought.com/products/epd/) and Helvetica Neue is recognized there. – Brian Feb 14 '14 at 14:48
  • 1
    Great, thanks. On what OS? On my EPD (Canopy actually) for Mac it works too, but on Ubuntu it's not working. – gozzilli Feb 14 '14 at 19:52
  • I'm on Ubuntu 12.04. I hope this helps. – Brian Feb 17 '14 at 16:39
10

Ubuntu 14.04 LTS

Upload the fonts

sudo cp NotoSansKR-Regular.otf /usr/share/fonts/

Update the cache of font

sudo fc-cache -fv

You can check the font list

fc-list

Restart ipython, etc. Check the font list

[f.name for f in matplotlib.font_manager.fontManager.ttflist]

Take a your font name

import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['font.family'] = 'Noto Sans Korean'

Draw

plt.title(u'한글 제목')
plt.xlabel(u'한글 축 이름')
plt.plot(range(5))
KyungHoon Kim
  • 2,859
  • 2
  • 23
  • 26
2

Kim already introduced dynamic solution works perfectly, and here's two other ways doing the same in static.

First, you may put a line to rc file for matplotlib . Refer to this page for more information about locating the file and detailed settings.

font.family : NanumGothic

Second, if you are working with ipython, you can put some commands for font setting to a configuration file for the interactive shell. Find the file named ipython_config.py which usually located under ~/.ipython/somewhere. Then add two more lines to the list, c.InteractiveShellApp.exec_lines .

c.InteractiveShellApp.exec_lines = [
    "import matplotlib as mpl",
    "mpl.rcParams['font.family'] = 'NanumGothic'"
]

Former always works whatever environment you run your shell script on as it loads the font when your script imports matplotlib .

Lyle
  • 1,238
  • 14
  • 16
1

The font cache shows up in a different place for me (.cache/matplotlib/fontList.cache). And before I actually had three of them in different places somehow :/

maybe try searching for it in your home directory:

find ~/ -name fontList.cache -exec rm {} \;
jmp
  • 203
  • 3
  • 7