How to customise legend?

5 Ansichten (letzte 30 Tage)
Herline van der Spuy
Herline van der Spuy am 26 Jul. 2021
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
colororder(clrs)
frequency = SE1Ofreq(:,i);
complex = SE1Ocomp(:,i);
scatter(frequency,complex,50,'filled','MarkerEdgeColor','black')
set(gca,'yscale','log')
set(gca,'xscale','log')
hold on
end
title('Master curve SE-1 Original')
ax = gca;
ax.FontSize = 14;
xlabel('\omega (rad/s)')
ylabel('G* (Pa)')
hold on
legend(temperatures)
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
end
grid on
ylim([0 90])
  3 Kommentare
Herline van der Spuy
Herline van der Spuy am 26 Jul. 2021
Oh, yeah that would probably be more helpful, sorry. I'll do it now.
Scott MacKenzie
Scott MacKenzie am 26 Jul. 2021
Bearbeitet: Scott MacKenzie am 26 Jul. 2021
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam Danz
Adam Danz am 26 Jul. 2021
Bearbeitet: Adam Danz am 26 Jul. 2021
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
colororder(clrs)
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.)
end
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
end
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 Kommentar
Herline van der Spuy
Herline van der Spuy am 26 Jul. 2021
This is brilliant! It does work perfectly.
Thank you so much.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by