2016-01-06 85 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