2016-01-06 76 views
1

我有問題可以用正確的比例繪製兩個數組。我使用dtw軟件包比較兩個數組x,y(https://pypi.python.org/pypi/dtw/1.0)。函數dtw返回一個矩陣和一個路徑。 用下面的代碼,我可以繪製矩陣和路徑:繪製曲線與動態時間變形矩陣對齊

import matplotlib.pyplot as plt 

dist, cost, acc, path = dtw(x, y, dist=lambda x, y: norm(x - y, ord=1)) 

plt.imshow(acc.T, origin='lower', cmap=cm.gray, interpolation='nearest') 
plt.colorbar() 
plt.plot(path[0], path[1], 'w') 

plt.ylim((-0.5, acc.shape[1]-0.5)) 
plt.xlim((-0.5, acc.shape[0]-0.5)) 

所得數值: enter image description here 然而,我想繪製對準它的兩條曲線,像示於(http://www.psb.ugent.be/cbd/papers/gentxwarper/DTWalgorithm.htm)。一條曲線位於矩陣的上方,另一條曲線位於左側​​,以便您可以比較哪些部分相等。

+0

絕對做,能。 [此示例](http://matplotlib.org/examples/pylab_examples/scatter_hist.html)具有不同的數據類型,但具有相似的佈局。也許你可以適應它? – kwinkunks

回答

3

像kwinkunks建議一樣(請參閱評論)我使用this example作爲模板。請注意,我使用「plt.pcolor()」而不是「plt.image()」來繪製矩陣。這是我的代碼和由此得出的數字:

''' 
Plotting 
''' 

nullfmt = NullFormatter() 

# definitions for the axes 
left, width = 0.12, 0.60 
bottom, height = 0.08, 0.60 
bottom_h = 0.16 + width 
left_h = left + 0.27 
rect_plot = [left_h, bottom, width, height] 
rect_x = [left_h, bottom_h, width, 0.2] 
rect_y = [left, bottom, 0.2, height] 

# start with a rectangular Figure 
plt.figure(2, figsize=(8, 8)) 

axplot = plt.axes(rect_plot) 
axx = plt.axes(rect_x) 
axy = plt.axes(rect_y) 

# Plot the matrix 
axplot.pcolor(acc.T,cmap=cm.gray) 
axplot.plot(path[0], path[1], 'w') 

axplot.set_xlim((0, len(x))) 
axplot.set_ylim((0, len(linear))) 
axplot.tick_params(axis='both', which='major', labelsize=18) 

# Plot time serie horizontal 
axx.plot(x,'.', color='k') 
axx.tick_params(axis='both', which='major', labelsize=18) 
xloc = plt.MaxNLocator(4) 
x2Formatter = FormatStrFormatter('%d') 
axx.yaxis.set_major_locator(xloc) 
axx.yaxis.set_major_formatter(x2Formatter) 

# Plot time serie vertical 
axy.plot(y,linear,'.',color='k') 
axy.invert_xaxis() 
yloc = plt.MaxNLocator(4) 
xFormatter = FormatStrFormatter('%d') 
axy.xaxis.set_major_locator(yloc) 
axy.xaxis.set_major_formatter(xFormatter) 
axy.tick_params(axis='both', which='major', labelsize=18) 

#Limits 
axx.set_xlim(axplot.get_xlim()) 
axy.set_ylim(axplot.get_ylim()) 

plt.show() 

enter image description here