Exporting a figure with an opaque legend box but no visible edge

조회 수: 5 (최근 30일)
Thomas Sykes
Thomas Sykes 2019년 4월 11일
댓글: Thomas Sykes 2019년 4월 11일
I want to make a figure whose legend has no visible border (so I set the legend edge colour to be white), but has an opaque background (so I do not turn the legend box off). Using the following code I can achieve this:
% Generate some data to plot
fakeXData = linspace(0,2*pi,100);
fakeYData1 = sin(fakeXData);
fakeYData2 = cos(fakeXData);
% Plot data
clf
plot(fakeXData, fakeYData1, 'r-', 'LineWidth', 2);
hold on
plot(fakeXData, fakeYData2, 'b-', 'LineWidth', 2);
% Legend
Lgd = legend({'Sine Curve','Cosine Curve'});
set(Lgd,'EdgeColor','white');
This successfully produces the figure I would like, as below:
matlabQ-window.png
I would then like to export the figure with my specified high resolution (i've used a low quality here for the purpose of uploading). To do this I use the following print command, since saveas outputs with screen resolution:
% Save image
print(gcf,'image','-dpng','-r600');
The figure produced is shown below, where notice the legend's box is back:
image.png
If I use saveas(gcf,'image.png') then I have the same issue (and can't control the dpi).
On the other hand, if I use file > saveas the figure is exported properly.
Thanks in advance for any advice on this.
  댓글 수: 2
Thomas Sykes
Thomas Sykes 2019년 4월 11일
편집: Thomas Sykes 2019년 4월 11일
I should note this issue also occurs on R2017a (the above graphs came from R2018a), either on Linux or Windows.

댓글을 달려면 로그인하십시오.

채택된 답변

Brendan Hamm
Brendan Hamm 2019년 4월 11일
You can modify this by setting the EdgeColor to none.
set(Lgd,'EdgeColor','none');

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by