How to make 2 legends

6 次查看(过去 30 天)
Hey,
I have a code as follows, which find a intersection of my function with y axis on the top and bottom. I would like to make 2 legends: one which corresponds to Temperature (now is creared), and the second which corresponds to the name of the sample, i.e. the type of point corresponds to sample{j} ). Does anybody have any suggestions?
close all
field = [0];
for j = 1:length(folderpath1)
for i = 1:length(specID1{1,j})
if (length(T1{i,j}.data) == 217 || length(T1{i,j}.data) == 237)
[minValue1(i,j),closestindex1N(i,j)] = min( abs( field - (H{i,j}( 1:1:((length(H{i,j})+1)/2)) )' ) ); % 15 kOe to -15 kOe
[minValue2(i,j),closestindex2(i,j)] = min( abs( field - (H{i,j}( ((length(H{i,j})+3)/2):1:end ) )' ) ) ; % -15 kOe to 15 kOe
closestindex2N(i,j) = (closestindex2(i,j) + ((length(H{1,1})+1)/2));
% find values of M for H=0 Oe, i.e. Mr
Mrplus(i,j) = M{i,j}(closestindex1N(i,j),1);
Mrminus(i,j) = abs(M{i,j}(closestindex2N(i,j),1));
Mrmean(i,j) = (Mrplus(i,j) + Mrminus(i,j))/2 ;
if str2double(TempLegend{i,j})==315
if i==1
PColor = [0 100 0]/256;
else
PColor = [0 255 0]/256;
end
elseif str2double(TempLegend{i,j})==80
PColor = 'b';
elseif str2double(TempLegend{i,j})==440
PColor = 'r';
end
if j==1; Point = 'o';
elseif j==2; Point = 's';
elseif j==3; Point = 'p';
elseif j==4; Point = 'd';
elseif j==5; Point = '*';
end
figure(10); grid on; grid minor
if j==2
plt2a(i,j) = plot(str2double(TempLegend{i,j}), Mrmean(i,j), Point,'Color',PColor,'MarkerSize',10,'displayname',[TempLegend{i,j} ' K']); hold on
else
plt2a(i,j) = plot(str2double(TempLegend{i,j}), Mrmean(i,j), Point,'Color',PColor,'MarkerSize',10,'handlevisibility','off'); hold on
end
legend('-DynamicLegend','location','northeast','fontsize',11,'box','off');
hold on; xlim([ 40 480 ]);
ylim([-0.1 2])
grid minor
end
end
end
delete(plt2a(5,2))
Plot looks like that:

采纳的回答

Adam Danz
Adam Danz 2019-11-4
编辑:Adam Danz 2019-11-5
There are few ways to create a pseudo legend. One way is to plot your data on the main axes but instead of creating a legend for that axes, you can create a 2nd invisible one that plots invisible markers containing your legend components. Then you can create a legend for the invisible axes and place it anywhere on your figure.
Here's a demo that is adaptable to your code.
% List all symbols that will represent the sample number
symbols = {'s' 'o' '^' 'p'}; % For samples 1 to 4
% List all temperatures and the colors for each temperature
temperatures = linspace(300,500,5);
colors = lines(5);
% Create the main figure. No need to assign display name since the
% legend will be based data from a different, invisible axis.
fh = figure();
axh = axes(fh);
hold(axh,'on')
for i = 1:4
for j = 1:5
plot(axh, rand()*10,rand()*10,'marker',symbols{i},'color', colors(j,:), 'LineStyle','none');
end
end
% Create invisible axis
pax = axes(fh,'position',[0 0 0 0]);
hold(pax,'on')
% Plot 1 non-existant data point per temperature color (all filled square markers)
arrayfun(@(j)plot(pax,nan,nan,'s','Color',colors(j,:),'MarkerFaceColor',colors(j,:),...
'DisplayName',sprintf('%.0f K', temperatures(j))), 1:numel(temperatures));
% Plot 1 non-existant data point per marker shape (all black color)
arrayfun(@(i)plot(pax,nan,nan,'Marker',symbols{i},'Color', 'k','LineStyle','none',...
'DisplayName',sprintf('sample %d', i)), 1:numel(symbols));
% Create legend for the hidden axes and place is somewhere on the figure
lh = legend(pax);
lh.Position(1) = .95 - lh.Position(3); % Places new legend 0.05 normalized units to the left or righ figure edge
lh.Position(2) = .95 - lh.Position(4); % Places new legend 0.05 normalized units under the top figure edge

更多回答(1 个)

Nadir Altinbas
Nadir Altinbas 2019-11-4
f = field[0]
j = 1:length(folderpath1)
plot(f,folderpath1);
hold on
i = 1:length(specID1{1,j})
plot(j,i)
legend('(i)',('j)')
note that to delete legend legend('off')
to add legend to axis legend(ax1,{'Line 1','Line 2','Line 3'})
legend('-DynamicLegend','location','northeast','fontsize',11,'box','off');

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by