exportgraphics streches images whily saving if 'visible' is 'off' - bug?

7 ビュー (過去 30 日間)
Adam Zabicki
Adam Zabicki 2022 年 1 月 17 日
回答済み: aamir ibrahim 2023 年 8 月 2 日
Hello,
I stumbled upon what might be a bug, or I am just overseeing some features...
When you run this code, the two saved PDFs should be the same because they only differ in their "visible" value. But if the figure's visibility is 'off', the saved PDF is distorted: The white space at the bottom is missing, and the content is streched to the bottom end.
Is this a bug?
clear all; close all;
%% on
f1 = figure('Visible','on');
heatmap(rand(10,5))
exportgraphics(f1,'test_on.pdf','ContentType','image');
%% off
f2 = figure('Visible','off');
heatmap(rand(10,5))
exportgraphics(f2,'test_off.pdf','ContentType','image');
edit: what is odd is, that this behavior is only seen when 'ContentType' is 'image'. Saving 'vector' graphics everything is ok.
  3 件のコメント
Adam Zabicki
Adam Zabicki 2022 年 1 月 24 日
no, the 'drawnow' command does not change the output PDF.
Trama Robin
Trama Robin 2022 年 11 月 4 日
I had the same issue.
In my code I have an if loop that is adding a patch on my figure only for some conditions, and the size was changing.
Adding a drawnow with an else statement and add an invisible patch even if I didn't need prevented the issue.
FYI, I am exporting in .tif, so it's an image and not a vector.

サインインしてコメントする。

回答 (3 件)

Walter Roberson
Walter Roberson 2022 年 1 月 23 日
Figure layout including axes layout (including xlim and ylim) is not performed until the figure visibility is turned on.
  1 件のコメント
Adam Zabicki
Adam Zabicki 2022 年 1 月 24 日
Hi,
thanks for your reply. May i have to add, that if you define the 'ContentType' to 'vector', both PDFs saved are identical. So it is the 'image' case only when the figure layout is not performed? Seems odd to me...

サインインしてコメントする。


yanqi liu
yanqi liu 2022 年 1 月 24 日
clear all; close all;
data = rand(10,5);
%% on
f1 = figure('Visible','on');
h1=heatmap(data);
exportgraphics(f1,'test_on.pdf','ContentType','image');
fa1 = getframe(f1);
%% off
f2 = figure('Visible','off');
h2=heatmap(data);
exportgraphics(f2,'test_off.pdf','ContentType','image');
fa2 = getframe(f2);
%% compare
figure;
subplot(1,2,1); imshow(frame2im(fa1));
subplot(1,2,2); imshow(frame2im(fa2));
yes,sir,now we can see the image is same,but export pdf when visible off is different,may be the axis do not set to equal in the code
  3 件のコメント
yanqi liu
yanqi liu 2022 年 1 月 24 日
yes,sir,may be figure display can make the image x、y axis equal,if not display,then the x、y axis not equal,so when get pdf print,it is mismatch
Adam Zabicki
Adam Zabicki 2022 年 1 月 24 日
But it works if 'ContentType' is set to 'vector'...

サインインしてコメントする。


aamir ibrahim
aamir ibrahim 2023 年 8 月 2 日
One workaround is to set the 'WindowState' for the figure to 'minimized'.
fig = figure;
fig.WindowState = 'minimized';
fig.Visible = 'on';
% Use exportgraphics
exportgraphics(fig);
close(fig);

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by