How to adjust legend

I would like to name all legends with a colored line, instead of only data1 and so on.
Furthermore i would like to exclude the legends with a triangle.
My code for the legend was:
legend('show')

2 件のコメント

Scott MacKenzie
Scott MacKenzie 2021 年 8 月 10 日
編集済み: Scott MacKenzie 2021 年 8 月 10 日
It might help if you post the code that generated the figure in your question.
Paul Hinze
Paul Hinze 2021 年 8 月 10 日
編集済み: Walter Roberson 2021 年 8 月 11 日
Indeed:
%%%%% ---- Plot loudness --- %%%%%
% Build nested for loop that iterates through each subject with their
% corresponding sounds:
% Make new figure and place on screen Breite/Höhe
hFig = figure( 2 );
set( hFig, 'Position', [400, 300, 1250, 700], 'Name', 'Loudness Curves');
% Define papersize for export
set( hFig, 'paperunits','centimeters','Paperposition',[ 0 0 10 4])
% Set Names of subjects:
names = {'Subject 1 Old','Subject 2 Old','Subject 3 Young','Subject 4 Young',...
'Subject 5 Young','Subject 6 Young','Subject 7 Young','Subject 8 Young'};
% Set 14 different colors:
C = {'b',[0 0.4470 0.7410],[0.3010 0.7450 0.9330],'c',[0.6350 0.0780 0.1840],'r',[0.8500 0.3250 0.0980],...
[0.9290 0.6940 0.1250],'m','k','g',[0 0 .2],[0 .1 .4],[.1 .2 .3]};
for i = 1:length(fn_subjects)
subplot(3,4,i)
for k = 1:length(fn_sound)
sounds = (vas_cell{i,1}.loudness(k).Sound);
level = (vas_cell{i,1}.loudness(k).Intensity);
vas = (vas_cell{i,1}.loudness(k).VAS);
subplot(3,4,i)
plotHandles = zeros(1,k);
hold on
plotHandles(k) = plot (level,vas,'color',C{k}, 'Linewidth',1,...
'DisplayName', 'Breaking Glass',...
'DisplayName', 'Car Horn',...
'DisplayName','Clapping');
title(names{i})
xlim([60 100])
xlabel('Level / dB SPL', 'FontSize', 12)
ylabel('Visual Analog Scale', 'FontSize', 12)
set(gca, 'FontSize', 12)
grid on
tone = AbsorbanceMEMR.(fn_subjects{i}).(fn_sound{k});
[FreqBand, Levels] = size(tone);
for j = 1:length(level)
maximum = max ( abs (tone(2:5,j)));
if maximum >= 0.03
plot(level(j), vas(j),'k^','MarkerSize',6)
end
end
end
sgtitle('Loudness')
end
lgd = legend('Breaking Glass','Car Horn','Clapping');
lgd.Position(1) = 0.375;
lgd.Position(2) = 0.235;
lgd.NumColumns = 8;
title(lgd, 'Sounds')

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

回答 (2 件)

Star Strider
Star Strider 2021 年 8 月 10 日

0 投票

I am not certain what you want, however the approach in Specify Legend Labels During Plotting Commands (using the 'DisplayName' name-value pair) could be appropriate.
.

10 件のコメント

Paul Hinze
Paul Hinze 2021 年 8 月 10 日
In the figure, you see data 1 to data 79, but i only want to have the data with a line and not with a triangle, furthermore i would like to give the data with a line a name.
I hope you understand me better
Star Strider
Star Strider 2021 年 8 月 10 日
See the documentation I linked to. By adding the 'DisplayName' name-value pair to the appropriate plot calls, then calling legend alone or with only the 'Location' (and certain other optional name-value pair arguments), the legend will only contain the lines/markers you want to display in it.
.
Paul Hinze
Paul Hinze 2021 年 8 月 10 日
Ok, cool thank you. I made a step forward.
But I still have problems getting the triangle to a line like in Breaking Glass.
Is it possible to neglect the triangle at the corresponding plot line?
Paul Hinze
Paul Hinze 2021 年 8 月 10 日
編集済み: Walter Roberson 2021 年 8 月 11 日
%%%%% ---- Plot loudness --- %%%%%
% Build nested for loop that iterates through each subject with their
% corresponding sounds:
% Make new figure and place on screen Breite/Höhe
hFig = figure( 2 );
set( hFig, 'Position', [400, 300, 1250, 700], 'Name', 'Loudness Curves');
% Define papersize for export
set( hFig, 'paperunits','centimeters','Paperposition',[ 0 0 10 4])
% Set Names of subjects:
names = {'Subject 1 Old','Subject 2 Old','Subject 3 Young','Subject 4 Young',...
'Subject 5 Young','Subject 6 Young','Subject 7 Young','Subject 8 Young'};
% Set 14 different colors:
C = {'b',[0 0.4470 0.7410],[0.3010 0.7450 0.9330],'c',[0.6350 0.0780 0.1840],'r',[0.8500 0.3250 0.0980],...
[0.9290 0.6940 0.1250],'m','k','g',[0 0 .2],[0 .1 .4],[.1 .2 .3]};
for i = 1:length(fn_subjects)
subplot(3,4,i)
for k = 1:length(fn_sound)
sounds = (vas_cell{i,1}.loudness(k).Sound);
level = (vas_cell{i,1}.loudness(k).Intensity);
vas = (vas_cell{i,1}.loudness(k).VAS);
plotHandles = zeros(1,k);
hold on
plotHandles(k) = plot (level,vas,'color',C{k}, 'Linewidth',1,...
'DisplayName', 'Breaking Glass',...
'DisplayName', 'Car Horn',...
'DisplayName','Clapping');
title(names{i})
xlim([60 100])
xlabel('Level / dB SPL', 'FontSize', 12)
ylabel('Visual Analog Scale', 'FontSize', 12)
set(gca, 'FontSize', 12)
grid on
tone = AbsorbanceMEMR.(fn_subjects{i}).(fn_sound{k});
[FreqBand, Levels] = size(tone);
for j = 1:length(level)
maximum = max ( abs (tone(2:5,j)));
if maximum >= 0.03
plot(level(j), vas(j),'k^','MarkerSize',6)
end
end
end
sgtitle('Loudness')
end
lgd = legend('Breaking Glass','Car Horn','Clapping');
lgd.Position(1) = 0.375;
lgd.Position(2) = 0.235;
lgd.NumColumns = 8;
title(lgd, 'Sounds')
Star Strider
Star Strider 2021 年 8 月 10 日
It is possible to select only the plotted lines/markers you want in the legend. See the documentation section Included Subset of Graphics Objects in Legend for details.
.
Paul Hinze
Paul Hinze 2021 年 8 月 10 日
thank you
Star Strider
Star Strider 2021 年 8 月 10 日
My pleasure!
.
Paul Hinze
Paul Hinze 2021 年 8 月 11 日
Hi Star Strider,
I tried many things with DisplayName and the objecthandle of the plot function in the describtion, you send me. The Example in the link seems also logical for me but in my code the solution still does not work. I have no clue how to solve this problem.
The main problems remain unsolved:
I want to suppres the the legend in the plot function, which is embedded in the if statement.
Sorry to bother you again.
Star Strider
Star Strider 2021 年 8 月 11 日
It seems Walter saw this before I did (sleeping) so I will not proceed further.
I could not run your code since I do not have the necessary data, and that makes it difficult to determine an appropriate solution. The best I could do was to refer to what were likely the most appropriate parts of the documentation, and then let you experiment with it.
.
Paul Hinze
Paul Hinze 2021 年 8 月 11 日
I know, its not a problem. In general the idea was good, and I mean it was late.
It is always cool to get help from you guys. I really appreciate it.

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

Walter Roberson
Walter Roberson 2021 年 8 月 11 日

0 投票

plotHandles(k) = plot (level,vas,'color',C{k}, 'Linewidth',1,...
'DisplayName', 'Breaking Glass',...
'DisplayName', 'Car Horn',...
'DisplayName','Clapping');
Only the last 'DisplayName' option in a plot() call with have any effect, even if the call is generating multiple lines.
You also really only want a summary legend, not a legend for each subplot. The easiest way to handle that is to fake it.
%%%%% ---- Plot loudness --- %%%%%
% Build nested for loop that iterates through each subject with their
% corresponding sounds:
% Make new figure and place on screen Breite/Höhe
hFig = figure( 2 );
set( hFig, 'Position', [400, 300, 1250, 700], 'Name', 'Loudness Curves');
% Define papersize for export
set( hFig, 'paperunits','centimeters','Paperposition',[ 0 0 10 4])
% Set Names of subjects:
names = {'Subject 1 Old','Subject 2 Old','Subject 3 Young','Subject 4 Young',...
'Subject 5 Young','Subject 6 Young','Subject 7 Young','Subject 8 Young'};
% Set 14 different colors:
C = {'b',[0 0.4470 0.7410],[0.3010 0.7450 0.9330],'c',[0.6350 0.0780 0.1840],'r',[0.8500 0.3250 0.0980],...
[0.9290 0.6940 0.1250],'m','k','g',[0 0 .2],[0 .1 .4],[.1 .2 .3]};
for i = 1:length(fn_subjects)
subplot(3,4,i)
for k = 1:length(fn_sound)
sounds = (vas_cell{i,1}.loudness(k).Sound);
level = (vas_cell{i,1}.loudness(k).Intensity);
vas = (vas_cell{i,1}.loudness(k).VAS);
hold on
plot (level,vas,'color',C{k}, 'Linewidth',1);
title(names{i})
xlim([60 100])
xlabel('Level / dB SPL', 'FontSize', 12)
ylabel('Visual Analog Scale', 'FontSize', 12)
set(gca, 'FontSize', 12)
grid on
tone = AbsorbanceMEMR.(fn_subjects{i}).(fn_sound{k});
[FreqBand, Levels] = size(tone);
for j = 1:length(level)
maximum = max ( abs (tone(2:5,j)));
if maximum >= 0.03
plot(level(j), vas(j),'k^','MarkerSize',6)
end
end
end
sgtitle('Loudness')
end
for K = 1 : 3
plotHandles(K) = plot(nan,nan,'color', C{K});
end
lgd = legend(plotHandles, {'Breaking Glass','Car Horn','Clapping'});
lgd.Position(1) = 0.375;
lgd.Position(2) = 0.235;
lgd.NumColumns = 8;
title(lgd, 'Sounds')

1 件のコメント

Paul Hinze
Paul Hinze 2021 年 8 月 11 日
Thank you for your help!
Now I maged to implement the code! And it works :)

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

カテゴリ

タグ

質問済み:

2021 年 8 月 10 日

コメント済み:

2021 年 8 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by