MATLAB Answers


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
m_grid('xtick',[0:60:360],'tickdir','out','ytick',[-90:30:90], 'color',dkgr, 'fontsize',8,'fontname','Times New Roman');
for j = 1:3
    group(j).lon(group(j).lon<0) = group(j).lon(group(j).lon<0) + 360;
    tm(j,:) = group(1).tm;
for j = 1:3
    proxycolor(j,:) = icon{j,1};
%The problem lies below here somewhere
legend boxoff

What am I doing wrong? Thanks in advance.



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)


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!