在 pandas 情节图例中显示非ASCII(日语)字符 [英] Display non ascii (Japanese) characters in pandas plot legend
本文介绍了在 pandas 情节图例中显示非ASCII(日语)字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我这样做:
import pandas as pd
pd.DataFrame( data=nr.random( (2,2) ), columns=[u'é',u'日本'] ).plot()
结果:
因此显示é
,但不显示日本
.稍作搜索后,我发现了这个页面,它似乎为matplotlib
.我在此处下载了字体文件,并使其与
So é
shows up, but not 日本
. After googling a bit, I found this page which seems to provide a solution for matplotlib
. I downloaded the font file here and got it to work with matplotlib
:
import matplotlib.font_manager as fm
prop = fm.FontProperties(fname='/Users/user/Downloads/IPAfont00303/ipag.ttf')
plt.plot( np.arange(10), np.arange(10), label=u'日本' )
plt.legend( prop=prop )
结果:
然后我尝试将相同的解决方案应用于pandas
:
Then I tried to apply the same solution to pandas
:
import matplotlib.font_manager as fm
prop = fm.FontProperties(fname='/Users/user/Downloads/IPAfont00303/ipag.ttf')
df0.plot( prop=prop )
结果:
TypeError: There is no line property "prop"
我理解错误消息,但是我不知道如何让熊猫使用prop=prop
.欢迎任何帮助.
I understand the error message, but I don't know how I can get pandas to use prop=prop
. Any help is welcome.
推荐答案
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
df = pd.DataFrame( data=np.random.random( (2,2) ), columns=[u'é',u'日本'] )
ax = df.plot()
legend = ax.legend()
font = font_manager.FontProperties(fname='/Users/user/Downloads/IPAfont00303/ipag.ttf')
for text in legend.texts:
text.set_font_properties(font)
plt.show()
这篇关于在 pandas 情节图例中显示非ASCII(日语)字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文