pythonpandasmatplotlibfontschinese-locale

How to display Chinese in matplotlib plot


Here, I have a plot work to do with pandas, like this :

most_active_posts.plot(x = 'title',y = 'active_span',kind = 'barh')

most_active_posts is an object of dataframe with index, I want a simple two-dimensional plot with two columns, one is 'title' and the other is 'active_span'.

title is type of string, which contains Chinese characters, while active_span is type of integer .

How can I display Chinese characters normally?


Solution

  • My work-around is like this:

    import pandas as pd
    import matplotlib.pyplot as plt
    import matplotlib.font_manager as fm
    font = fm.FontProperties(fname='c:\\windows\\fonts\\simsun.ttc')  # speicify font
    ax = most_active_posts.plot(x = 'title',y = 'active_span',kind = 'barh')
    ax.set_xticklabels(most_active_posts['title'].str.decode('utf-8'), fontproperties=font)
    plt.show()
    

    Basically, you need to specify a valid font for Chinese characters.