Python matplotlib 默认不支持中文。代码中出现中文的话,图片中会显示乱码。其实这是由于matplotlib默认的字体库中,没有中文的字体库引起的。只要指定机器上的中文字体,即可解决这个问题。
下面是一段示例代码,在Python3.4下运行通过。
# -*- coding=utf-8 -*-
from matplotlib import font_manager
import matplotlib.pyplot as plt;
if __name__ == '__main__':
thread_num = [1, 3, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
throughput1 = [574.8695765, 878.0209408, 890.9727752, 892.219843, 890.1647918, 886.6329007, 884.769794, 882.6222708,
880.9602467, 877.2218384, 875.8964252, 875.2639468, 874.2978296]
throughput2 = [18.22837677, 52.29950462, 88.11752763, 161.328, 235.0830, 233.0830, 231.0830, 229.0830, 227.0830,
225.0830, 223.0830, 221.0830, 219.0830062]
# 指定字体为宋体
zh_font = font_manager.FontProperties(fname=r'c:\windows\fonts\simsun.ttc', size=14)
# Plot the figure.
line, = plt.plot(thread_num, throughput1, color='cornflowerblue', linestyle='-', linewidth=3, marker='o', markersize=8,
label='大对象')
plt.plot(thread_num, throughput2, color='darkred', linestyle='-', marker='s', markersize=8, linewidth=3,
label='小对象')
plt.ylim(ymin=0)
plt.ylabel('平均吞吐率(Mbit/s)', fontproperties=zh_font)
plt.legend(loc=(0.78, 0.8), prop=zh_font)
plt.xlabel('线程数', fontproperties=zh_font)
plt.savefig('TestConcurrentPut10Mand10K.png')
plt.show()
程序运行结果如下: