imshow: labels as any arbitrary function of the image indices

只谈情不闲聊 提交于 2019-12-02 09:12:42

问题


imshow plots a matrix against its column indices (x axis) and row indices (y axis). I would like the axes labels to not be indices, but an arbitrary function of the indices.

e.g. pitch detection

imshow(A, aspect='auto') where A.shape == (88200,8)

in the x-axis, shows several ticks at about [11000, 22000, ..., 88000] in the y-axis, shows the frequency bin [0,1,2,3,4,5,6,7]

What I want is:

x-axis labeling are normalized from samples to seconds. For a 2 second audio at 44.1kHz sample rate, I want two ticks at [1,2].

y-axis labeling is the pitch as a note. i want the labels in the note of the pitch ['c', 'd', 'e', 'f', 'g', 'a', 'b'].

ideally:

imshow(A, ylabel=lambda i: freqs[i], xlabel=lambda j: j/44100)


回答1:


You can do this with a combination of Locators and Formatters (doc).

ax = gca()
ax.imshow(rand(500,500))

ax.get_xaxis().set_major_formatter(FuncFormatter(lambda x,p :"%.2f"%(x/44100)))
ax.get_yaxis().set_major_locator(LinearLocator(7))
ax.get_yaxis().set_major_formatter(FixedFormatter(['c', 'd', 'e', 'f', 'g', 'a', 'b']))
draw()


来源:https://stackoverflow.com/questions/12645946/imshow-labels-as-any-arbitrary-function-of-the-image-indices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!