1. 程式人生 > >python3繪圖示例3(基於matplotlib:折線圖等)

python3繪圖示例3(基於matplotlib:折線圖等)

xtend 繪圖 def usr n) noise leg flat float

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from pylab import *
from numpy import *
import numpy

# 數據點圖-數據點平滑處理
def moveing_average(ineterval,window_size):
window=ones(int(window_size))/float(window_size)
return convolve(ineterval,window,‘same‘)

t=linspace(-4,4,100)
y=sign(t)+randn(len(t))*0.1
plot(t,y,‘k.‘)


y_av=moveing_average(y,10)
plot(t,y_av,‘r‘)

xlabel(‘time‘)
ylabel(‘value‘)
grid(True)
show()

# 圖2-一個為曲線圖 一個為折線圖
windows=[‘flat‘,‘hanning‘,‘hamming‘,‘bartlett‘,‘blackman‘]

def smooth(x,window_len=11,window=‘hanning‘):
if x.ndim!=1:
print(‘ere‘)

if x.size<window_len:
print(‘ee2‘)

if window_len<3:
return x

if not window in windows:
print(‘4‘)

s=numpy.r_[x[window_len-1:0:-1],x,x[-1:-window_len:-1]]

if window==‘flat‘:
w=numpy.ones(window_len,‘d‘)
else:
w=eval(‘numpy.‘+window+‘(window_len)‘)
y=numpy.convolve(w/w.sum(),s,mode=‘valid‘)
return y

t=linspace(-4,4,100)
x=sign(t)
xn=x+randn(len(t))*0.1

y=smooth(x)

ws=31
subplot(211)
plot(ones(ws))

for w in windows[1:]:
eval(‘plot(‘+w+‘(ws))‘)
axis([0,30,0,1.1])
legend(windows)
title(‘smoothing‘)

subplot(212)
plot(x)
plot(xn)
for w in windows[1:]:
plot(smooth(xn,10,w))
I=[‘original ‘,‘noise‘]
I.extend(windows)
legend(I)

title(‘signal‘)
show()


python3繪圖示例3(基於matplotlib:折線圖等)