Legend being occupied by background spectrogram

조회 수: 4 (최근 30일)
Sreeraj T
Sreeraj T 2020년 11월 19일
댓글: Sreeraj T 2020년 11월 19일
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

채택된 답변

VBBV
VBBV 2020년 11월 19일
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
Sreeraj T
Sreeraj T 2020년 11월 19일
Thanks you.
It worked for question 1. Any thoughts on second question?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 11월 19일
편집: KALYAN ACHARJYA 2020년 11월 19일
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
Sreeraj T
Sreeraj T 2020년 11월 19일
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).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by