How do I get a legend with custom colours in a loop plot?

1 次查看(过去 30 天)
I'm plotting some data within a for loop using the following code:
for k = 1:13 % there is some code here to loop through some files
Y = {'Intensity T=10','Intensity T=15','Intensity T=20','Intensity T=25','Intensity T=30','Intensity T=35','Intensity T=40','Intensity T=45','Intensity T=50','Intensity T=55','Intensity T=60','Intensity T=65','Intensity T=70'};
fig1 = figure('name',Y{k},'numbertitle','off');
for ij = 1:6
x = 0:0.2:9.6;
plot(x,fliplr(totalinmodesarray),'k.--'); % Additional plot in specified colour
y = fliplr(intensitymatrix(ij,2:2:end));
intensities{ij} = y;
plot(x,y,'.--')
xlabel('Current (mA)')
ylabel('Power (mW)')
hold on
end
legend('Total','mode 1', 'mode 2', 'mode 3', 'mode 4', 'mode 5', 'mode 6','Location','best')
saveas(gcf, Y{k}, 'fig')
end
This simple way of doing the legend worked fine until I added in another plot that I wanted to be in a specified colour rather than the default matlab colours. This gave me the wrong colours in the legend labels, such that several of them didn't match the lines. Do I need to incorporate the legend into the loop to solve this?

回答(1 个)

madhan ravi
madhan ravi 2018-12-13
编辑:madhan ravi 2018-12-13
similiar to figure names define the colours in a cell array and access them in the plot command with loop iterators as the index

类别

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