Show the value at hovered position (x,y) with interactive pcolormesh plots
07:05 23 Dec 2025

I find the coordinates shown below (in the form: (x, y) [z]) very useful for interacting with the data. Unfortunately, librosa.display.specshow does not show the value at (x, y), rather, only the coordinates. The underlying function is plt.pcolormesh. How can I achieve a similar behavior?

%matplotlib widget

import librosa
import matplotlib.pyplot as plt
import numpy as np

y, sr = librosa.load(librosa.ex('trumpet'))
D = librosa.stft(y)
mag = np.abs(D)

plt.figure()
plt.subplot(2,1,1)
plt.title('Using plt.imshow')
plt.imshow(librosa.amplitude_to_db(mag, ref=np.max), aspect='auto', origin='lower', cmap='magma')
plt.subplot(2,1,2)
plt.title('Using librosa.display.specshow')
librosa.display.specshow(librosa.amplitude_to_db(mag, ref=np.max), sr=sr, y_axis='log', x_axis='time', cmap='magma')
plt.tight_layout()
plt.show()

matplotlib figure with 2 subplots, generated by the code above and hovering on the first subplot

matplotlib figure with 2 subplots, generated by the code above and hovering on the second subplot

python matplotlib ipympl