Python matplotlib支持中文
2016-07-05 16:07
495 查看
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()
程序运行结果如下:
下面是一段示例代码,在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()
程序运行结果如下:
相关文章推荐
- python的那些事——join()和"+"
- 我的Python第一篇 输入基础篇
- 使用Python将HTML转成PDF
- 在VS Code上搭建Python开发环境
- Python的“绑定机制”(self)
- python import cv2报错 segmentation fault:11
- Python subprocess模块学习总结
- 你会用Python写洗脑神曲吗?
- Numpy基础:数组和矢量计算
- python json.load 的奇葩错误
- python基础之使用os.system来执行系统命令
- python基础之使用os.system来执行系统命令
- python检测文件的MD值
- python——list
- 用python修改haproxy配置文件
- keras包安装
- ZigZag Conversion(python)
- python之禅
- python 利用文件锁单例执行脚本
- ZeroMQ - 三种模型的python实现