我在以正确的缩放比例绘制两个数组时遇到问题。我使用 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))
结果图: 但是,我想绘制与其对齐的两条曲线,如 ( http://www.psb.ugent.be/cbd/papers/gentxwarper/DTWalgorithm.htm ) 中所示。一条曲线位于矩阵上方,另一条曲线位于左侧,以便您可以比较哪些部分相等。
最佳答案
就像 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()
关于python - 绘制与动态时间扭曲矩阵对齐的曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34633721/