通过 pip
安装 PyplotZ
库
pip install pyplotz
代码
from pyplotz.pyplotz import PyplotZ
pltz = PyplotZ()
pltz.enable_chinese()
most_active_posts.plot(x='title',y='active_span',kind='bar')
pltz.xticks(np.arange(len(df.cn)),df.cn,rotation=360)
pltz.legend()
pltz.show()
结果如图
引用:
https://stackoverflow.com/questions/39630928/how-to-plot-a-figure-with-chinese-characters-in-label
https://stackoverflow.com/questions/21307832/how-to-display-chinese-in-pandas-plot/47345983#47345983
或者另外一种方式,直接编写matplotlib的原生接口
测试1
import matplotlib.pyplot as plt
# 你的字体路径
font_path = "Songti.ttc"
font = matplotlib.font_manager.FontProperties(fname=font_path)
# 绘图
plt.text(0.5, 0.5, s=u'测试中文显示', fontproperties=font)
plt.show()
输出为:
第二段代码
import matplotlib.pyplot as plt
import numpy as np
# 加载字体
font_path = "Songti.ttc"
font = matplotlib.font_manager.FontProperties(fname=font_path)
# 绘图数据
t = np.arange(0.01, 5.0, 0.01)
s = np.exp(-t)
plt.plot(t, s)
# 设置绘图
plt.xlim(5, 0)
plt.ylabel(u'电压 voltage (mV)', fontproperties=font)
plt.title(u'应该要增长吧 Should be growing...', fontproperties=font)
plt.grid(True)
plt.show()
输出为