Why does legend patch turn black when using saveas?

3 views (last 30 days)
Hi, I have a problem with the function saveas in matlab. The problem appears when I create a legend for more than one graph (eg 2 line graphs) in an image and then uses saveas on the figure. The problem is that when I try to use saveas (or publish, which uses saveas) on the image and the legend patch (the object named rectangle) is set to white, the patch turns black in the image (for me a .png image).
Since I wanted to set the background of the legend object to semi-transparent I had to remove the box and this causes some problems. The details will be seen in the example code. I am not sure whether there is a problem for all types of images, but I know it is at least a problem for .png images.
Have anyone seen this problem before and is it a known fix or workaround for it?
Example code:
publish('testScript');
function testScript()
figure;
plot(1:10,2:11,1:10,1:10,'r','LineW',2);
legend({'leg1','leg2'});
setLegendAlpha(0.9);
function setLegendAlpha( alphaValue )
hleg=findall(gcf,'tag','legend');
set(hleg,'box','off');
ch=findall(hleg,'type','axes');
ah=annotation('rectangle',get(ch,'position'));
set(ah,'facecolor','w','facealpha',alphaValue);
uistack(hleg,'top');
I am not sure if this problem is due to some error in the code in the function setLegendAlpha or if it is a bug. Also, if someone knows a workaround that does not cause the legend background to turn black when saving with saveas I would be happy. I can add that this problem cannot be seen in the figure and if I save it using file->save this problem is not seen either.
Matlab 2014a, windows 7
  1 Comment
Patrik Ek
Patrik Ek on 23 Oct 2014
Edited: Patrik Ek on 23 Oct 2014
If this is a known bug or is unsupported I would also appreciate a comment or so. In both cases it would be worth sending a bug report on and also add that this is something that would be valuable in future releases. I think that it is a usual that legends become to long or to large.

Sign in to comment.

Answers (0)

Categories

Find more on Printing and Saving in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!