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 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()


 

猜你喜欢

转载自blog.csdn.net/hyichao_csdn/article/details/45293407