MATLAB: Plot and legend colors don’t match

colorlegendplot

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

Best Answer

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.