Python畫曲線圖(論文,報告等常用)
阿新 • • 發佈:2018-11-01
<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 python import pylab as pl import numpy as np filename1 = '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()