Display non ascii (Japanese) characters in a pandas story 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, édisplayed, but not 日本. After a bit of searching, 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 do not know how I can use pandas to use prop=prop. Any help is appreciated.

+4
source share
1 answer
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()
+2
source

Source: https://habr.com/ru/post/1537368/


All Articles