フィルターのクリア

Specify wich values appear on a plot legend

10 ビュー (過去 30 日間)
Juan Sanchez
Juan Sanchez 2018 年 8 月 29 日
編集済み: Juan Sanchez 2018 年 8 月 29 日
I'm plotting in a for loop a series of signals and additionally lines to separate segments in the plot. I want to add a Legend for each of the signal but when I add the legend it adds the lines too and Im not really sure how to take the lines out of the Legend.
My code is:
for j=1:8
figure(j)
for i=1:6
plot(f(j,:,i),cxy(j,:,i))
hold on
xlim([0 45])
line ([4,4],[0, 1],'Color','red','LineStyle','--') % Cada linea separa las bandas de energia para ver la coherencia entre bandas
line ([8,8],[0, 1],'Color','red','LineStyle','--')
line ([13,13],[0, 1],'Color','red','LineStyle','--')
line ([30,30],[0, 1],'Color','red','LineStyle','--')
% Here the legend should appear only for the plots not for the lines
legend('Signal 1','Signal 2','Signal 3','Signal 4','Signal 5', 'Signal 6')
end
end
My result is the plot I attach. I've searched arround how to solve this but nothing is working.
Thanks in advanced for the help and sorry for the basic question.
  2 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 8 月 29 日
"how to take the lines out of the Legend?"
Which lines are you talking?
Are these lines within legend dashed colors lines?
Juan Sanchez
Juan Sanchez 2018 年 8 月 29 日
編集済み: Juan Sanchez 2018 年 8 月 29 日
Yes sorry if I didnt explain well, the lines:
if true
line ([4,4],[0, 1],'Color','red','LineStyle','--') % Cada linea separa las bandas de energia para ver la coherencia entre bandas
line ([8,8],[0, 1],'Color','red','LineStyle','--')
line ([13,13],[0, 1],'Color','red','LineStyle','--')
line ([30,30],[0, 1],'Color','red','LineStyle','--')
end
These dotted lines appear in the legend

サインインしてコメントする。

採用された回答

dpb
dpb 2018 年 8 月 29 日
編集済み: dpb 2018 年 8 月 29 日
Use the handles of the lines you wish to add legends to...
nFig=8; % set the numbers via variables
nLin=6; % instead of using "magic" numbers...
hL=gobjects(nFig,nLin); % preallocate for the line handles
for j=1:nFig
figure(j)
for i=1:nLin
hL(j,i)=plot(f(j,:,i),cxy(j,:,i)); % plot the line, save the line handle for it...
hold on
xlim([0 45])
line ([4,4],[0, 1],'Color','red','LineStyle','--') % Cada linea separa las bandas de energia para ver la coherencia entre bandas
line ([8,8],[0, 1],'Color','red','LineStyle','--')
line ([13,13],[0, 1],'Color','red','LineStyle','--')
line ([30,30],[0, 1],'Color','red','LineStyle','--')
% Here the legend should appear only for the plots not for the lines
end
% Add legend after the lines are drawn using the handles of those wanted to show only
legend(hL(j,:),{'Signal 1','Signal 2','Signal 3','Signal 4','Signal 5', 'Signal 6'})
end
One could also build the legend text dynamically to match the loop indices instead of hardcoding...
  2 件のコメント
Juan Sanchez
Juan Sanchez 2018 年 8 月 29 日
編集済み: Juan Sanchez 2018 年 8 月 29 日
Sir, most appreciated. The magic numbers correspond to the dimensions of the data but anyway you are right. As for the code in:
legend(hL(i,:).... should be replaced by a j since they are 6 plots.
Anyway Thank you for the clear explanation! Most appreciated.
dpb
dpb 2018 年 8 月 29 日
編集済み: dpb 2018 年 8 月 29 日
You're right, got the loop indices reversed...I'll make the correction, thanks.
If don't code with "magic numbers" as general rule it makes modifying/using code for other purposes much easier or makes the given code useful if the present data sizes change for any reason.
I wasn't really suggesting to just move the constants to explicit variables; I just used that as a shortcut/prompt, what I really meant was to do something like
[nFig,nLin]=size(cxy);
THEN, if you get a dataset for some reason that isn't exactly 8x6 you still don't have to do anything but run the code to process it.
It's just a good habit to develop.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by