Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display non ascii (Japanese) characters in pandas plot legend

If I do this:

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

Result:

enter image description here

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 )

Result:

enter image description here

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 )

Result:

TypeError: There is no line property "prop"

I understand the error message, but I don't know how I can get pandas to use prop=prop. Any help is welcome.

like image 345
usual me Avatar asked Apr 21 '14 12:04

usual me


1 Answers

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()
like image 120
unutbu Avatar answered Nov 14 '22 23:11

unutbu