MATLAB Answers

Adam

Need help with plotting; colors disappear after setting the legend

Asked by Adam
on 29 Mar 2013

I'm trying to create a multi-colored plot, but once I set the location for the legend, the colors on the plot and on the legend revert to black.

If I remove the part of the code for the legend, the colors on the plot remain fine, so I assume the error lies in setting the legend.

Here's the code:

tm = zeros(3,group(1).ny);
hl = zeros(3,1); 
proxycolor = zeros(3,3);
icon{1,1}= [1 0 0];   icon{1,2}= '^'; icon{1,3} = 'Raw Data';
icon{2,1}= [1 1 0];   icon{2,2}= 'v'; icon{2,3} = 'Quality Controlled Data';
icon{3,1}= [0 1 0];   icon{3,2}= '*'; icon{3,3} = 'Screened Data';
fig('Data Availability'),clf
subplot(3,1,1:2)
m_proj('Robinson','clong',180);
m_grid('xtick',[0:60:360],'tickdir','out','ytick',[-90:30:90], 'color',dkgr, 'fontsize',8,'fontname','Times New Roman');
m_coast('color','k');
for j = 1:3
    group(j).lon(group(j).lon<0) = group(j).lon(group(j).lon<0) + 360;
    tm(j,:) = group(1).tm;
end
for j = 1:3
    proxycolor(j,:) = icon{j,1};
    hl(j)=m_line(group(j).lon,group(j).lat,'color',icon{j,1},'marker',icon{j,2},'MarkerFaceColor',icon{j,1},'MarkerSize',7,'LineStyle','none');
end
%The problem lies below here somewhere
[LEGH,OBJH,OUTH,OUTM]=legend(hl(:),icon{:,3});pause; 
set(LEGH,'FontName','Times','FontSize',10);
set(OUTH,'Color','k','MarkerFaceColor','k','Markersize',8);
legend boxoff

What am I doing wrong? Thanks in advance.

  0 Comments

Products

1 Answer

Answer by Walter Roberson
on 29 Mar 2013

The third output from legend(), the one you are setting the Color of, is the handles of the objects for which the legend was created, not the handles of the example lines created by legend. With the code you have, you are setting the color of the original lines. You should instead

set( findobj(OBJH,'Type','line'), 'Color', 'k', 'MarkerFaceColor', 'k', 'Markersize', 8)

  0 Comments


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today