使用Python 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。
參考: