如何在matplotlib里显示中文

在matplotlib中,对中文的支持并不是很友好,需要配置中文字体,步骤如下:

  1. 导入相应的包:
    import matplotlib.pyplot as plt
    import matplotlib

  2. 加载相应的中文字符
    zhfont1 = matplotlib.font_manager.FontProperties(fname=’/usr/share/fonts/cjkuni-ukai/ukai.ttc’)
    注意,如果要尝试其他字体,可以在/usr/share/fonts路径下找

  3. 在需要用到中文的地方,加上该字体属性
    plt.title(u”中文”, fontsize=18,fontproperties=zhfont1)
    注意:中文字符串前要有一个u

你可能感兴趣的:(python)