Stack Exchange
Draw vertical lines on matlab spectrogram plot
Does the matlab spectrogram function lock the created figure in anyway? I want to draw vertical lines on the figure but the line function does not seem to do anything. How can I draw a line on a matlab spectrogram?
clc; clear all; close all;
[data, fs, nbits] = wavread(<INSERT WAVE FILE HERE>);
% [data, fs, nbits] = wavread('white_0.05_6sec_aud.wav');


N_data=length(data); N_frame=128; N_half=N_frame/2; N_loop=N_data/(N_half);
output=zeros(N_data,1);
hz=0:(fs/2)/N_half:(fs/2)-(fs/2)/N_half;

spectrogram(data, hanning(N_frame), N_half, N_frame, fs);

x = [6500 6500];
y = [0 5.5];

H = gca;

% set(gca, 'NextPlot', 'add');
% line(x, y);

h = line([6500, 6500], [0, 5.5]);
set(h, 'parent', handles.predicted_ax);

% view(-90,90)
% set(gca,'ydir','reverse')
% 
% [y, x] = ginput(1)


% view(-90, 180);


Do you have some code that creates spectograms that we can test with?

I am just using the spectrogram() function, however I have added my code to the question. Please comment and uncomment the line drawing functions as per needed; they don't seem to do anything here

I can make this code work by getting rid of the extra options you pass to spectrogram (weird) and also replacing real data with random noise (really weird).

What do you mean by extra options? For my work the the 2nd to last parameters to the function spectrogram() should be exactly as shown above, which is different than their default values I think. So I must get it to work for the parameters shown above. What I really want to know is whether the spectrogram() function locks the figure or the axes so that nothing else can be drawn on them, and if so, how to unlock it

The spectogram generates a surf and sets the view to (0,90). The surf sets the zlim to some values (dependent on the spectrogram data), and apparently, adding a line to the current plot does not change the zlim (probably because spectrogram locked the axes somehow; should be findable in edit spectrogram). Therefore, in view(0,90), the line completely disappears (rotate the plot; you'll see the line appear somewhere above the surface). 
To resolve: the way you add a line defaults to the line having z-coordinates of [0 0], 
which, for many spectrograms, will be above the range of the axes set by spectrogram. 
Issuing
zl = zlim;
axis([xlim ylim zl(1) max(0, zl(2))])
view(0,90)

after the line should then make the line appear. 
Also: in my case, the surface over which the line was hovering was mostly blue, as was the line. This doesn't help making it apparent there is a line :) I made it white, which contrasted better with the blue/yellow/red surf below. 

Thanks a lot!!!!! I don't understand all the details about surfs and zlim but your code makes the line appear

Very strange. Before coming here, I tried using plot3() creating a line with a z value extremely high and it didn't work, but this did the trick.Kudos and +1.
