在pandas plot legend中显示非ascii(日语)字符

时间:2014-04-21 12:07:26

标签: python unicode matplotlib pandas

如果我这样做:

import pandas as pd
pd.DataFrame( data=nr.random( (2,2) ), columns=[u'é',u'日本'] ).plot()

结果:

enter image description here

所以é显示,但不显示日本。谷歌搜索后,我发现这page似乎为matplotlib提供了解决方案。我下载了字体文件here,并将其与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 )

结果:

enter image description here

然后我尝试将相同的解决方案应用于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"

我理解错误消息,但我不知道如何让pandas使用prop=prop。欢迎任何帮助。

1 个答案:

答案 0 :(得分:3)

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