MATLAB: Problem with markers & dashed lines in figures containing both plot and fill

filllegendlinestylesMATLABplot

The legend gets somewhat distorted in plots that contain both lines and fill objects. For example, compare the legend produced by this:
figure;
plot(linspace(0,pi,25),cos(linspace(0,pi,25)),'s--','LineWidth',2,'MarkerFaceColor','b');
hold on;
plot([0 pi],[0.5 0.5],'-or','LineWidth',2,'MarkerFaceColor','r');
h = fill([0 pi pi 0],[1 1 -1 -1],'g','FaceAlpha',0.15);
hA = get(h,'Annotation');
hA.LegendInformation.IconDisplayStyle = 'off';
legend({'Cosine','Constant'},'interpreter','latex');
with the output when we don't have the fill object:
figure;
plot(linspace(0,pi,25),cos(linspace(0,pi,25)),'s--','LineWidth',2,'MarkerFaceColor','b');
hold on;
plot([0 pi],[0.5 0.5],'-or','LineWidth',2,'MarkerFaceColor','r');
legend({'Cosine','Constant'},'interpreter','latex');
Any solution would be appreciated.
EDIT: At first, I didn't realize that the markers on the plot lines also get distorted as well. So I guess, this whole problem is caused by fill.

Best Answer

This is a problem with the opengl renderer. When you have a transparency, you are using opengl, and it can be buggy. To see the difference, click on that figure then:
set(gcf,'render','painters')
Notice the legend looks fine but your transparency has gone away. I am not sure if there is a fix for this.