在 matplotlib 中显示外语(在虚拟环境中) [英] Display foreign language in matplotlib (in virtual env)

查看:33
本文介绍了在 matplotlib 中显示外语(在虚拟环境中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Jupyter 中使用 matplotlib,我的数据包括韩语字符.Matplotlib 尚不支持韩文字符,因此建议我手动在 matplotlib 上设置字符.在没有设置字体的情况下,我的示例图会取代方形空框.

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

根据互联网上的几个例子,这是我所做的.

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

  1. 下载韩文字体 (.ttf)
  2. 将字体复制到我的字体目录 ('/Users/Library/Fonts')
  3. 重启 Jupyter 内核并测试

我再次运行了我的 matplotlib,看看结果是否正确,但我仍然得到空框.

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

这是我在 Jupyter 中运行的代码.

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)

这是错误信息.

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

所以这就是我认为我做错的地方.我没有将字体文件放在我的 Jupyter 的正确目录中,它在我机器上的 virenv 上运行.我的问题是这个目录在哪里?我检查了font_manager.py"文件,但我无法从中得到太多.该文件说我的操作系统字体目录确实是

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

用户/图书馆/字体

感谢您的帮助!提前谢谢你!

I appreciate any help! Thank you in advance!

推荐答案

结果比我想象的要简单.正如@ImportanceOfBeingErnest 上面提到的,关键是:

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

  1. 安装字体
  2. 清除字体缓存

就我而言,我无法找到具有字体缓存的正确目录.我从这个帖子中得到了帮助.

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

  1. 下载您想要的字体.把它放在字体目录中.就我而言,它是用户/图书馆/字体/...
  2. 找到字体缓存所在的目录.

在 Jupyter 中,在您的单元格中执行此操作.

In Jupyter, execute this in your cell.

from matplotlib import font_manager
fm = mpl.font_manager
fm.get_cachedir()
>> '/Users/.matplotlib'

  1. 然后转到该目录并删除该文件

转到 shell 并执行此命令.

Go to shell and execute this command.

rm fontList.py3k.cache

现在,当我执行 matplotlib 时,会显示正确的字体.

Now when I execute matplotlib, the correct font shows.

这篇关于在 matplotlib 中显示外语(在虚拟环境中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆