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
453 views
in Technique[技术] by (71.8m points)

fonts - Display foreign language in matplotlib (in virtual env)

I am working with matplotlib in Jupyter, and my data includes Korean characters. Matplotlib does not support Korean character yet, so I was advised to set character on matplotlib manually. Without setting the font, my sample graph displaces square empty boxes.

Sample plot using Korean characters.

objects = ('??', '?', '?', '???', '???', '??')
y_pos = np.arange(len(objects))
performance = [10,8,6,4,2,1]

plt.bar(y_pos, performance, align='center', alpha=0.5)
plt.xticks(y_pos, objects)
plt.ylabel('Usage')
plt.title('Programming language usage')

plt.show()

enter image description here

Following few examples on the internet, here's what I have done.

  1. Download Korean font (.ttf)
  2. Copy the font in my Font directory ('/Users/Library/Fonts')
  3. Restart Jupyter kernel and test

And I ran my matplotlib once more to see if it would turn out correctly, but I still get empty boxes.

This is the code I ran in Jupyter.

from matplotlib import font_manager, rc
font_name = font_manager.FontProperties(fname = '/Users/Library/Fonts/custom/NanumBarunGothic.ttf').get_name()    
rc('font', family = font_name)

And this is the error message.

/Users/anaconda/envs/my_vir_env/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['NanumBarunGothic'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

So this is what I think I am doing wrong. I am not putting the font file in the right directory for my Jupyter, which is running on virenv on my machine. My question is where is this directory? I checked 'font_manager.py' file but I could not get much out of it. The file says that my OS font directory is indeed

User/Library/Fonts

I appreciate any help! Thank you in advance!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

It turned out to be simpler than I thought. As mentioned above by @ImportanceOfBeingErnest, the key is to:

  1. Install the fonts
  2. Erase font cache

In my case, I was not able to find the right directory that had font cache. I got help from this post.

  1. Download font you want. Place it in font directory. In my case it was, Users/Library/Fonts/...
  2. Find the directory where your font cache resides.

In Jupyter, execute this in your cell.

from matplotlib import font_manager
fm = mpl.font_manager
fm.get_cachedir()
>> '/Users/.matplotlib'
  1. Then go to that directory and delete the file

Go to shell and execute this command.

rm fontList.py3k.cache

Now when I execute matplotlib, the correct font shows.


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

...