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

조회 수: 1 (최근 30일)
JJH
JJH 2018년 12월 13일
댓글: JJH 2018년 12월 13일
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
  댓글 수: 1
JJH
JJH 2018년 12월 13일
How does this work when I have two plot commands in the loop?

댓글을 달려면 로그인하십시오.

카테고리

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