Some legend properties are reset to default when printing figure
Show older comments
I am printing a figure into the -depsc format using the print() command. The figure has two subplots, with a legend in the upper plot. An example of the code I'm using to generate the plot is below:
figure1=figure;
set(gcf,'Position',[100 100 1100 650])
set(gcf,'PaperPositionMode','auto','DefaultAxesFontname','TimesNewRoman')
subplot1=subplot(2,1,1,'Parent',figure1,'FontSize',12);
hold(subplot1,'all');
box(subplot1,'off');
plot(x1,y1,'Parent',subplot1,'Color','k','LineStyle','-','DisplayName','Field')
plot(x2,y2,'Parent',subplot1,'Color','k','LineStyle','--','DisplayName','Model')
subplot2=subplot(2,1,2,'Parent',figure1,'FontSize',12);
hold(subplot2,'all');
box(subplot2,'off');
plot(x3,y3,'Parent',subplot2,'Color','k','LineStyle','-')
plot(x4,y4,'Parent',subplot2,'Color','k','LineStyle','--')
legend1=legend(subplot1,'show');
set(legend1,'Location','NorthWest','EdgeColor',[1 1 1],'Color',[0.5 0.5 0.5]);
Then, to print the figure in color eps format, I use the following command:
print(figure1,'-depsc','-r300','Figure1.eps');
The problem is that, even though I specify 'EdgeColor' [1 1 1] and 'Color' [0.5 0.5 0.5] in the Legend properties, these are not honored when printing. The EdgeColor in the printed file is black and the background Color of the legend is reset to white. But for some reason, the property setting the Location to NorthWest is honored and NOT reset to the default, NorthEast.
I thought maybe it was only honoring the first property for some reason, so I deleted the Location property, making the EdgeColor property the first one in the list. However, the EdgeColor in the printed .eps file was still black...
Anybody know the solution to this problem? Is this an unnoticed/unresolved bug?? I am using Matlab_R2013a, in case that makes any difference in solving this problem.
Thanks!
3 Comments
Matt Kistler
on 6 Jun 2014
I am having a similar issue with retaining some of the axes properties with a call to print(), specifically with the location of the x-axis label, and Position property of the XTickLabels.
To adjust for large strings used in the XTickLabels, I had to move the xLabel down quite a bit so they did not overlap. This was accomplished by:
xlabel('Hour (EDT)','FontSize',fontSize,'Position',[166.861 -33 0])
When I call print()
print(gcf,'-dpng',fig_name)
The x-axes return the location of the xLabel and XTickLabels to their default positions. I found this to occur in the low-level hardcopy() function, in which there is very little documentation (or code displayed for editing).
I found a workaround by adding buffer text to both the xLabel and xTickLabels, so that when the image renders, it looks how I want. I don't think this workaround suits your needs though, but wanted to express that the underlying problem is still there and I would like to see if anyone has other options?
Thanks.
Jan Vábek
on 7 Apr 2020
Hello,
any progress on this issue? It seems to be persistent even in R2019a. I'm trying to save a png file with a different text color in the legend. I've a correct output on the screen and also when I use "File" -> "Save As..." However, the color is reset to default using the print command.
Dan Fries
on 22 May 2020
I am having the same issue. Is there any way to work around this or fix it?
Answers (1)
Walter Roberson
on 22 May 2020
2 votes
I think you seeing the effect of InvertHardcopy figure property; https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html#buiwuyk-1-InvertHardcopy
1 Comment
Dan Fries
on 22 May 2020
Thanks for looking into this. Unfortunately, even setting InvertHardcopy to 'off' does not help. The rough code I am using is below. The moment "print" is called the linestyle oin the legend is reverted to default, i.e. from '-' to '--', but I would like it to be '-' in the legend.
Any ideas?
figure
fig1 = gcf;
fig1.Units = 'inches';
size_width = 8;
size_height = 4;
fig1.Position = [5 0 size_width size_height];
hold on
a = plot(x1,y1,'--');
b = plot(x2,y2,'--');
c = plot(x3,y3,'--');
d = plot(x4,y4,'--');
[hLgd, oLgd] = legend([a b c d],{'X1' 'X2' 'X3' 'X4'},'Location','Southeast');
lineh = findobj(oLgd,'type','line');
set(lineh,'LineStyle','-')
cd(saveDir)
figure(fig1)
set(gcf,'InvertHardcopy','off')
fig1.PaperUnits = 'inches';
fig1.PaperPosition = [0 0 size_width size_height];
fig1.PaperPositionMode = 'manual';
fig1.PaperSize = [size_width size_height];
print(fig1,'FigureTest','-depsc','-r300')
print(fig1,'FigureTest','-dpdf','-r300')
print(fig1,'FigureTest','-dpng','-r300')
savefig('FigureTest')
Categories
Find more on Graphics Object Properties 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!