python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python Matplotlib绘图并输出图像到文件中

利用Python Matplotlib绘图并输出图像到文件中的方式

作者:肖永威

这篇文章主要介绍了利用Python Matplotlib绘图并输出图像到文件中的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

Matplotlib绘图并输出图像到文件中

在大数据及深度学习的背景下,随着卷积神经网络(CNN)的成功应用,图像识别能力好像唾手可得。

最近实际工作中,却遇到了困难,难题是用于可学习的图像贫乏,很难形成用于学习的样本。

其实,也是有一定解决图像的方法,用现有的数据,形成图形文件,供深度学习使用。

通过了解,发现Python中的matplotlib可以快速绘图,并形成文件。

matplotlib是基于Python语言的开源项目,旨在为Python提供一个数据绘图包。

matplotlib的pyplot子库提供了和matlab类似的绘图API,方便用户快速绘制2D图表。

可以实现:创建图表、绘图区域、画线、添加图示标签等。

注意:pyplot中的 “轴域”(axes)是指图形的一部分(两条坐标轴围成的区域),而不是指多于一个轴(more than one axis)的严格数学术语。

安装Matplotlib,通常使用pip命令,本文是使用下载whl压缩包的方式,也可以使用在线安装方方式。

(注:在线方式,在CMD窗口下,执行python -m pip install -U pip setuptools进行升级。

接着键入python -m pip install matplotlib进行自动的安装,系统会自动下载安装包。)

D:\Python>pip install matplotlib-2.2.2-cp36-cp36m-win_amd64.whl

这里写图片描述

关于中文字符集需要处理方式之一

如下:

plt.rcParams['font.sans-serif'] = ['SimHei']  # 中文字体设置  
plt.rcParams['axes.unicode_minus'] = False 

以绘制示功图为例,这样图形是提供给技术人员分析使用,类似医院中CT片,给专业医生分析、识别疾病使用。

这些图像往往都是数字化设备采集数据后,经过算法处理后显示出图像的,按此策略也可以生成图像文件。

很幸运,Python的matplotlib提供类似Matlib能力的开源包,可以模拟出所需要的图像。

'''
Created on 2018年4月14日
@author: XiaoYW
'''
import matplotlib.pyplot as plt
x = [0.00,0.00,0.01,0.01,0.02,0.04,0.05,0.07,0.10,0.12,0.15,0.18,0.21,0.24,0.28,0.32,0.37,0.42,0.46,0.52,0.57,0.62,0.68,0.74,0.80,0.86,0.92,0.99,1.06,1.12,1.19,1.26,1.33,1.40,1.48,1.68,1.75,1.82,1.88,1.95,2.01,2.08,2.15,2.21,2.28,2.35,2.41,2.48,2.55,2.61,2.68,2.75,2.81,2.88,2.95,3.01,3.08,3.15,3.21,3.27,3.34,3.39,3.46,3.51,3.58,3.64,3.69,3.75,3.81,3.86,3.92,3.97,4.02,4.08,4.13,4.17,4.22,4.27,4.31,4.36,4.41,4.44,4.49,4.52,4.56,4.60,4.64,4.67,4.71,4.74,4.77,4.80,4.82,4.85,4.87,4.89,4.91,4.93,4.94,4.96,4.97,4.98,4.99,4.99,4.99,4.99,4.99,4.99,4.98,4.97,4.96,4.94,4.93,4.91,4.88,4.86,4.83,4.80,4.77,4.73,4.70,4.66,4.62,4.57,4.52,4.46,4.42,4.36,4.29,4.24,4.18,4.11,4.06,3.99,3.92,3.85,3.78,3.70,3.63,3.55,3.48,3.41,3.33,3.26,3.18,3.09,3.02,2.94,2.85,2.78,2.69,2.61,2.54,2.45,2.37,2.30,2.21,2.13,2.06,1.98,1.89,1.82,1.74,1.67,1.59,1.52,1.45,1.37,1.30,1.23,1.16,1.09,1.03,0.96,0.90,0.84,0.78,0.72,0.67,0.61,0.55,0.51,0.45,0.41,0.36,0.32,0.28,0.24,0.21,0.18,0.14,0.12,0.09,0.07,0.05,0.04,0.02,0.01,0.01,0.00,0.00]
y = [35.01,35.30,35.32,35.22,37.23,38.91,40.61,41.66,43.01,45.78,49.20,51.85,53.81,56.15,58.65,57.61,55.97,54.22,52.13,50.91,51.01,51.65,52.28,53.65,54.56,54.53,54.43,53.75,52.45,51.85,51.76,51.75,51.80,52.42,52.42,52.47,52.60,52.75,52.83,52.55,52.35,52.25,52.01,51.82,51.82,51.81,51.85,51.88,51.88,51.81,51.80,51.75,51.53,51.49,51.54,51.51,51.51,51.52,51.51,51.48,51.52,51.26,51.09,51.05,50.92,50.93,50.97,50.97,50.95,51.02,50.99,51.04,51.04,50.92,50.65,50.64,50.61,50.61,50.66,50.67,50.64,50.67,50.58,50.47,50.45,50.24,50.07,50.10,50.07,50.05,50.11,50.10,50.07,49.97,49.70,49.67,49.68,49.50,49.50,49.49,49.47,49.50,49.46,49.48,49.21,48.11,47.81,47.37,47.32,46.85,45.77,44.54,43.09,41.66,40.29,38.49,36.54,33.99,31.23,28.23,25.26,23.25,24.20,26.10,29.01,31.74,33.24,33.20,32.61,30.41,27.65,26.16,25.95,25.98,27.61,29.39,31.12,31.89,31.97,30.75,29.65,28.33,27.31,27.00,27.47,28.33,29.30,30.26,30.96,30.99,30.31,29.17,28.83,28.18,28.16,28.18,28.94,29.49,30.08,30.34,30.43,30.24,29.58,29.15,29.08,29.08,29.41,29.76,30.36,30.48,30.55,30.48,30.47,30.14,29.80,29.80,30.17,30.39,30.85,31.42,31.55,31.53,31.54,31.48,31.43,31.40,31.41,31.57,32.01,32.66,33.24,33.25,33.24,33.24,32.80,32.25,32.25,32.40,32.61,33.04,35.01]
miny = 30.0  # 下限载荷
maxy = 51.0  # 上限载荷
plt.axis([0, 5, 0, 60]) # 最大坐标视窗
plt.plot(x,y,color="Blue")
plt.axhline(miny,color="Red")    # 画参考线方法一
plt.axhline(maxy,color="Red")    # 画参考线方法一
x2 = [0,5]
y2 = [20,20]
plt.plot(x2,y2,color="Blue")     # 画参考线方法二
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.title("ergograph 示功图")
plt.xlabel("x value")
plt.ylabel("y value")
plt.show()
# plt.savefig("D:/temp.png")

示例代码中 plt.savefig("D:/temp.png") ,很容易的把图像保存到文件中,注意如果直接生产文件时,plt.show()函数就不需要了,

Demo执行结果如下:

这里写图片描述

代码中“matplotlib.pyplot.plot(*args, **kwargs)”是画线函数,是一个灵活的命令,它的参数可以是任意数量,

比如:

    plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

这表示的是(x,y)对,(1,1)(2,4)(3,9)(4,16)。这里有第三个可选参数,它是字符串格式的,表示颜色和线的类型。

该字符串格式中的字母和符号来自于MATLAB,它是颜色字符串和线的类型字符串的组合。

默认情况下,该字符串参数是’b-‘,表示蓝色的实线。  

注意:输出图像到文件中的格式有,emf、 eps、 pdf、 png、 ps、 raw、 rgba、 svg、 svgz。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文