Need help with plotting; colors disappear after setting the legend
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Walter Roberson
2013-3-29
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!