Plot and legend colors don't match

I suspect it's because I'm using transparency (i.e. FaceAlpha) in my plot. Example code run on MATLAB r2013a:
clear all, close all
x = 1:10;
y = 100:100:200;
z1 = 1*x;
z2 = 3*x;
[X,Y] = meshgrid(x,y);
Z = [z1;z2];
fig = 0; fig = fig + 1; figure(fig);
hMesh = mesh(X,Y,Z);
set(gca,'XLim',[1 10],...
'YLim',[100 200],...
'XTick',[0:1:10],...
'YTick',[100:100:200]);
set(hMesh,'FaceColor',[1 0 0],'EdgeColor',[1 0 0],'FaceAlpha',0,'EdgeAlpha',0.4,'LineWidth',3);
hLegend = legend('Test','Location','Northeast');
box on

 採用された回答

José-Luis
José-Luis 2014 年 8 月 28 日
編集済み: José-Luis 2014 年 8 月 28 日

0 投票

legendObj = get(hLegend,'Children');
your_patch = findobj(legendObj,'Type','patch');
set(your_patch,'FaceAlpha',0,'EdgeAlpha',0.4);
Please accept the answer that best solves your problem.

1 件のコメント

RG
RG 2019 年 1 月 29 日
Hi, there, I'm trying to solve similar issue, legend colors to match the plot line colors and can't find a solution, so I'm asking for help here. Here is the code:
Color = distinguishable_colors(numel(donuts));
for m = 1 : max(data.numberOfDonutsPerShelf)-1
i(m) = m;
i = i';
end
for n = 1 : numel(donuts)
hi(n,:) = plot(i,data.donutsFrequencies(:,n),'-o','color',Color(n,:));
end
hold on
legend(hi,strcat(num2str(donutsShelves),'N'))
legend boxoff

サインインしてコメントする。

その他の回答 (1 件)

Chad Greene
Chad Greene 2014 年 8 月 28 日

1 投票

You could use legalpha.

1 件のコメント

Gunnar
Gunnar 2014 年 8 月 28 日
編集済み: Gunnar 2014 年 8 月 28 日
Thanks, Chad! Great script, though I choose the answer below since it provides no dependencies such as an extra script.
Hopefully, MATLAB can correct this in a future version of MATLAB.

サインインしてコメントする。

カテゴリ

質問済み:

2014 年 8 月 28 日

コメント済み:

RG
2019 年 1 月 29 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by