使用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()
|
本文由用戶 lsssw12313 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!