使用python中的matplotlib进行绘图分析数据
matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图。而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中。
它的文档相当完备,并且 Gallery页面 中有上百幅缩略图,打开之后都有源程序。因此如果你需要绘制某种类型的图,只需要在这个页面中浏览/复制/粘贴一下,基本上都能搞定。
在Linux下比较著名的数据图工具还有gnuplot,这个是免费的,Python有一个包可以调用gnuplot,但是语法比较不习惯,而且画图质量不高。
而 Matplotlib则比较强:Matlab的语法、python语言、latex的画图质量(还可以使用内嵌的latex引擎绘制的数学公式)。
快速绘图
matplotlib的pyplot子库提供了和matlab类似的绘图API,方便用户快速绘制2D图表。例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | # coding=gbk '' ' Created on Jul 12 , 2014 python 科学计算学习:numpy快速处理数据测试 @author : 皮皮 '' ' import string import matplotlib.pyplot as plt import numpy as np if __name__ == '__main__' : file = open(E:machine_learningdatasetshousing_datahousing_data_ages.txt, 'r' ) linesList = file.readlines() # print(linesList) linesList = [line.strip().split(,) for line in linesList] file.close() print(linesList:) print(linesList) # years = [string.atof(x[ ]) for x in linesList] years = [x[ ] for x in linesList] print(years) price = [x[ 1 ] for x in linesList] print(price) plt.plot(years, price, 'b*' )#,label=$cos(x^ 2 )$) plt.plot(years, price, 'r' ) plt.xlabel(years(+ 2000 )) plt.ylabel(housing average price(* 2000 yuan)) plt.ylim( , 15 ) plt.title( 'line_regression & gradient decrease' ) plt.legend() plt.show() |