How do I get a legend with custom colours in a loop plot?
2 ビュー (過去 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?
0 件のコメント
回答 (1 件)
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 Center および File Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!