Legend being occupied by background spectrogram
4 views (last 30 days)
Show older comments
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/419193/image.jpeg)
My question is:
- 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?
- 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
0 Comments
Accepted Answer
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
More Answers (1)
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?
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!