Imshow: labels like any arbitrary image index function

imshow displays the matrix relative to the column indices (x axis) and row indices (y axis). I would like the axis labels not to be indices, but an arbitrary index function.

eg. pitch determination

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

on the x axis, shows several ticks around [11000, 22000, ..., 88000] on the y axis, shows the frequency bit [0,1,2,3,4,5,6,7]

I want:

The x axis designation is normalized from samples to seconds. For a 2-second sound at a sampling frequency of 44.1 kHz, I want two ticks at [1,2] .

The y axis designation is a step as a note. I need the marks in the pitch note ['c', 'd', 'e', 'f', 'g', 'a', 'b'] .

perfectly:

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

+1
matplotlib axis-labels
source share
1 answer

You can do this with a combination of Locator and Formatter (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() 
+1
source share

All Articles