Legend being occupied by background spectrogram

2 views (last 30 days)
I have code which goes like this:
clc;clear;
close all
Time=linspace(16.8,17.8,230400)';
Field=linspace(0,4,230400)';
figure('units','normalized','outerposition',[0 0 1 1])
hammng_wndw_size=4096;
window=hamming(hammng_wndw_size); %window size
noverlap=512; % the noverlaps its the no. of points for repeating the window
nfft=4096; %size of fft
fs=32; %sampling freq
[Sp,F,T,P]=spectrogram(Field,window,noverlap,nfft,fs,'yaxis');
T_forspectrogrm=T./3600+Time(1);
surf(T_forspectrogrm,F,10*log10(P),'edgecolor','none','FaceColor','interp');
axis tight;ylim([0 4]);view(0,90);
colormap(jet);
cb = colorbar(gca,'east');
cb.Position = cb.Position + [0.07 0 0 0];
cb.Label.String = '\bf log_{10}(nT^2/Hz)';
ylabel('Frequency (Hz)')
set(gca,'linewidth',1.5,'fontsize',...
12,'Layer','top');
grid off
hold on
Field4fig=Field(1:round(length(Field)/length(T)):length(Field),1);
plot(T_forspectrogrm,Field4fig,'r',...
T_forspectrogrm,Field4fig/2,'k','LineWidth',1.5)
legend({'A','B'},'FontSize',15,'Location','northwest')
ax=gca;
ax.XTickLabel=datestr(hours(ax.XTick),'HH:MM');
And whose output is like this:
My question is:
  1. As you can see from the command, I have 2 legends in the program, however, in the plot only one is showing, the other is occupied by the background spectrogram. What am I doing it and how can I rectify it?
  2. If you click on the ‘Data Cursor’ and click on the lines, we can see that the end points of both the lines is 16.82 and 18.78. How can I modify the code to start x axis from this time in hh:mm format in such a way that there are 10 evenly placed ticks in x axis?
I am usign MATLAB2016a

Accepted Answer

VBBV
VBBV on 19 Nov 2020
hold on
Field4fig=Field(1:round(length(Field)/length(T)):length(Field),1);
F = plot(T_forspectrogrm,Field4fig,'r',...
T_forspectrogrm,Field4fig/2,'k','LineWidth',1.5)
legend(F, {'A','B'},'FontSize',15,'Location','northwest')
Try above for plotting legend
  1 Comment
Sreeraj T
Sreeraj T on 19 Nov 2020
Thanks you.
It worked for question 1. Any thoughts on second question?

Sign in to comment.

More Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 19 Nov 2020
Edited: KALYAN ACHARJYA on 19 Nov 2020
Questions 1:
You have only one legened in the provided code
legend({'A','B'},'FontSize',15,'Location','northwest')
Question 2:
Clarification needed: Do you want to start the line plot from 17:00 right?
  1 Comment
Sreeraj T
Sreeraj T on 19 Nov 2020
Thank you for taking time to look into my problem.
  1. I beleive that I am giving the legends. You may try this portion
plot(T_forspectrogrm,Field4fig,'r',...
T_forspectrogrm,Field4fig/2,'k','LineWidth',1.5)
legend({'A','B'},'FontSize',15,'Location','northwest')
of code and see that I have indeed given two legends.
2. I need to start from hh:mm corresponding to 16.82, if thats possible and divide the x axis into 10 evenly space part (I have modified the question).

Sign in to comment.

Products


Release

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!