Clear Filters
Clear Filters

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

1 view (last 30 days)
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?

Answers (1)

madhan ravi
madhan ravi on 13 Dec 2018
Edited: madhan ravi on 13 Dec 2018
similiar to figure names define the colours in a cell array and access them in the plot command with loop iterators as the index

Community Treasure Hunt

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

Start Hunting!