How to customise legend?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Scott MacKenzie
2021-7-26
编辑: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
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
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)])
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!