我在绘制具有正确缩放比例的两个数组时遇到问题。我使用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)所示。一条曲线位于矩阵上方,另一条曲线位于左侧,因此您可以比较哪些部分相等。
答案 0 :(得分: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()