This is due to a bug when using transparency in MATLAB. OpenGL does not render the borders correctly.
This issue occurs when a plot with some transparency has its edges overlapping with the edge of the figure.
Possible Workaround on Mac:
Increase the axes offset by a small value depending on the scale of your figure. For example:
figure
area(0:10,0:10)
box on
alpha(0.2)
set(gca,'xlim',[0 10.001])
set(gca,'ylim',[-0.001 10])
Possible workaround on Windows and Linux:
1. OpenGL is the renderer that MATLAB uses to handle transparency. By default, OpenGL runs in hardware acceleration mode to increase performance. Use OpenGL in software mode instead:
Make sure to run this command before opening a new figure.
2. Once OpenGL is set to run in software mode, slightly increase the x-axis limit to make the right border reappear:
figure
area(1:10,1:10)
box on
alpha(0.2)
xlim([1 10.001])
This should also fix issues with the borders of the legend box.
Best Answer