標籤:python 畫圖
<pre name="code" class="python">在很多時候,例如寫論文,例如寫報告,例如做ppt,都需要花很多很多曲線圖,讓人家信服畢竟資料視覺效果是人的本能。假如讀者您很不幸,像我一樣不會用matlab之類的東西畫圖或者沒辦法用matlab畫圖,那麼可以稍微關注一下python,因為python裡面有很強大的庫matplotlib,讓使用者直接用terminal就可以做大部分matlab畫圖能做的事情。matplotlib的安裝,可以先安裝pip然後sudo pip install matplotlib假如,現在任務是,從三個文字文件裡面讀入資料,然後畫出曲線圖然後比較,那麼就可以像如下代碼一樣做。。
<pre name="code" class="python">#!/usr/bin/env pythonimport pylab as plimport numpy as npfilename1 = 'NUMBERCUTOUT0'filename2 = 'NUMBERCUTOUT1'filename3 = 'NUMBERCUTOUT2'file1 = open(filename1,'r')file2 = open(filename2,'r')file3 = open(filename3,'r')value1 = []value2 = []value3 = []for word in file1:value1.append(word[:-1])for word in file2:value2.append(word[:-1])for word in file3:value3.append(word[:-1])length = len(value1)X = np.linspace(0,length,length,endpoint=True)fig = pl.figure(1)pl.plot(X,value1, color ='blue', linewidth = 1.0, linestyle =':',label='Straight-forward Structure')pl.plot(X,value2, color ='green',linewidth=1.0, linestyle='-', label='Single-branch Structure')pl.plot(X,value3, color ='red', linewidth=1.0, linestyle='--',label='Double-branch Structure')pl.legend(loc='upper right')pl.title('SingleBranch(Blue) DoubleBranch(Green) TribleBranch(Red)')pl.show()
Python畫曲線圖(論文,報告等常用)