问题描述
我正在使用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.
- 下载朝鲜语字体(.ttf)
- 将字体复制到我的Font目录('/Users/Library/Fonts')中
- 重新启动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:
- 安装字体
- 删除字体缓存
就我而言,我找不到具有字体缓存的正确目录.我从此帖子中获得了帮助.
In my case, I was not able to find the right directory that had font cache. I got help from this post.
- 下载所需字体.将其放在字体目录中.就我而言,它是Users/Library/Fonts/...
- 找到字体缓存所在的目录.
在Jupyter中,在您的单元格中执行此操作.
In Jupyter, execute this in your cell.
from matplotlib import font_manager
fm = mpl.font_manager
fm.get_cachedir()
>> '/Users/.matplotlib'
- 然后转到该目录并删除文件
转到shell并执行此命令.
Go to shell and execute this command.
rm fontList.py3k.cache
现在,当我执行matplotlib时,将显示正确的字体.
Now when I execute matplotlib, the correct font shows.
这篇关于在matplotlib中显示外语(在虚拟环境中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!