python: 使用matplotlib的pyplot繪製圖表

工做中須要觀察數據的變化趨勢,用python寫了一段小程序來用顯示簡單圖表,分享出來方便有一樣需求的人,matplotlib是個很不錯的庫。html

#!encode=utf8
from matplotlib import pyplot as plt
import sys
ignore_num=(int)(sys.argv[1])
data=sys.argv[2]
show_type=0
if len(sys.argv)>3:
    show_type=(int)(sys.argv[3])
x=[]
valid_ppl=[]
train_ll=[]
valid_ll=[]
fi=open(data,"r")
line=fi.readline()
n=0
while line:
    if line.find("Values") >= 0:
        arr=line.split(" ")
        n+=1
        if n>ignore_num:
            x.append(n)
            train_ll.append(arr[6])
            valid_ll.append(arr[8])
            valid_ppl.append(arr[9])
    line=fi.readline()
if show_type==0:
    plt.plot(x,train_ll,color="blue",label="train_ll")
    plt.plot(x,valid_ll,color="red",label="valid_ll")
    plt.plot(x,valid_ppl,color="orange",label="valid_ppl")
elif show_type==1:
    plt.plot(x,train_ll,color="blue",label="train_ll")
    plt.plot(x,valid_ll,color="red",label="valid_ll")
else:
    plt.plot(x,valid_ppl,color="orange",label="valid_ppl")
plt.legend(loc='upper right')
plt.show()

使用cmd命令窗口運行程序python

運行結果:小程序

參考資料:http://www.scipy-lectures.org/intro/matplotlib/matplotlib.html  這個網頁裏寫了pyplot的詳細用法,以及如何用python繪製更豐富的圖表,是很是好的學習資料。app

相關文章
相關標籤/搜索