Legend being occupied by background spectrogram

2 ビュー (過去 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 Center および File ExchangeLegend についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by