MATLAB Answers

How to customise legend?

조회 수: 5(최근 30일)
Herline van der Spuy
Herline van der Spuy 2021년 7월 26일
댓글: Herline van der Spuy 2021년 7월 26일
This is probably a very stupid question, but how do I fix this?
Like data1 corresponds to the dark blue of -24°C, but I want the diamonds as one colour, aka orange. So my question is,
How do I adjust the legend so that it shows those 10 temperatures, but only one orange diamond?
I have tried this:
legend(temperatures,{'δ data'})
but the array shows complications.
Here is the code, sorry if it's "messy" or confusing.
for i = 1:10
frequency = SE1Ofreq(:,i);
complex = SE1Ocomp(:,i);
hold on
title('Master curve SE-1 Original')
ax = gca;
ax.FontSize = 14;
xlabel('\omega (rad/s)')
ylabel('G* (Pa)')
hold on
yyaxis right
ylabel('δ (°)')
for j = 1:10
frequency1 = SE1Ofreq(:,j);
phase1 = SE1Ophas(:,j);
scatter(frequency1,phase1,50,[0.8500 0.3250 0.0980],'d','filled','MarkerEdgeColor','black')
hold on
grid on
ylim([0 90])
  댓글 수: 3
Scott MacKenzie
Scott MacKenzie 2021년 7월 26일
I didn't say explicitly, but I meant "code that can be executed"; i.e., if the data are not embedded in the code, then post the data as well.

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

채택된 답변

Adam Danz
Adam Danz 2021년 7월 26일
편집: Adam Danz 2021년 7월 26일
A clean and efficient approach is to assign the display names to the scatter objects and then specify the handles to include in the legend. This not only avoids the problem of duplicate legend entries but it also directly pairs the objects with names.
Unable to test due to missing variable definitions.
n = 10; % added
scatObj = gobjects(1,n); % added
ax = gca(); % added
set(ax,'yscale','log') % moved / changed
set(ax,'xscale','log') % moved / changed
hold(ax, 'on') % moved / changed
for i = 1:n % changed
frequency = SE1Ofreq(:,i);
complex = SE1Ocomp(:,i);
scatObj(i) = scatter(ax,frequency,complex,50,'filled',... % moved / changed
'MarkerEdgeColor','black', ...
'DisplayName', temperatures{i}); % Add legend name here (I assume temperatures is a cell str.)
title(ax, 'Master curve SE-1 Original') % added axis handle
% ax = gca; % remove, redundant
ax.FontSize = 14;
xlabel(ax,'\omega (rad/s)') % added axis handle
ylabel(ax,'G* (Pa)') % added axis handle
% hold on % remove, redundant
% legend(ax, temperatures) % do this at the end
yyaxis(ax, 'right') % added axis handle
ylabel(ax,'δ (°)') % added axis handle
m = 10; % added
scatObj2 = gobjects(1,m) % added
for j = 1:m % changed
frequency1 = SE1Ofreq(:,j);
phase1 = SE1Ophas(:,j);
scatObj2(j) = scatter(ax, frequency1,phase1,50,... % changed
[0.8500 0.3250 0.0980],'d','filled',...
'MarkerEdgeColor','black', ...
'DisplayName', 'InsertName');
% hold on % remove, redundant
grid(ax,'on') % added axis handle
ylim(ax,[0 90]) % added axis handle
% Add legend
% Include all handles in scatObj and only the first handle in scatObj2.
legend(ax, [scatObj, scatObj2(1)])
  댓글 수: 1
Herline van der Spuy
Herline van der Spuy 2021년 7월 26일
This is brilliant! It does work perfectly.
Thank you so much.

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

추가 답변(0개)




Community Treasure Hunt

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

Start Hunting!

Translated by