Problems printing graphics in pdf format

Since some time ago the graphs generated in matlab are saved empty in pdf format with the print function. This happens especially with the heatmap type

6 Comments

What if anything changed (about your MATLAB code, about your MATLAB installation, about your computer, etc.) between the last time the graphs you exported were exported successfully and the first time they were exported empty?
What release of MATLAB are you using and what operating system?
Can you show a small (no more than a dozen lines or so) that generate an empty PDF when you export? Including the call to print will let us make sure you're using it in a way that is expected to work on your release.
Can you make sure you haven't overloaded the print function? What does this command show?
which -all print
/MATLAB/toolbox/matlab/graphics/graphics/printing/print.m print is a Java method % com.mathworks.hg.util.InfoPanel method print is a Java method % com.mathworks.mwswing.MJPanel method print is a Java method % javax.swing.JPanel method print is a Java method % javax.swing.JComponent method print is a Java method % java.awt.Container method print is a Java method % java.awt.Component method /MATLAB/toolbox/matlab/automation/core/+matlab/+automation/+streams/ToStandardOutput.m % matlab.automation.streams.ToStandardOutput method /MATLAB/toolbox/matlab/automation/core/+matlab/+automation/+streams/OutputStream.m % matlab.automation.streams.OutputStream method print is a Java method % com.mathworks.hg.peer.HeavyweightLightweightContainerFactory$FigurePanelContainerLight method print is a Java method % com.mathworks.toolbox.sl3d.vrcanvas.VRGLCanvas method print is a Java method % com.jogamp.opengl.awt.GLCanvas method print is a Java method % java.awt.Canvas method /MATLAB/toolbox/mbc/mbctools/@modeldev/print.m % modeldev method /MATLAB/toolbox/mbc/mbctools/@mdev_local/print.m % mdev_local method
I have updated the Matlab version to R2024a
This is an example of the code:
figure(ifig)
heatmap(M_matrix_up,[],[],[],'TickAngle', 45, 'Colorbar',true,'Colormap', 'jet',...
'MinColorValue', min(min(TRATIO)), 'MaxColorValue', max(max(TRATIO)));
set(gca,'YTick',1:m);
set(gca,'XTick',1:m);
set(gca,'XTickLabel',states,'Fontsize',10);
set(gca,'YTickLabel',states,'Fontsize',10);
title(strcat('Map of Warming Dominance for the',{' '},char(quantiles(j))))
set( gcf,'PaperSize',[29.7 21.0], 'PaperPosition',[0 0 29.7 21.0])
print(figure(ifig),strcat('Heat_map_US_',char(quantiles(j)),'_1950_2021'),'-dpdf')
print(figure(ifig),strcat('Heat_map_US_',char(quantiles(j)),'_1950_2021'),'-dpng')
print(figure(ifig),strcat('Heat_map_US_',char(quantiles(j)),'_1950_2021'),'-deps')
Better would be
fig = figure(ifig);
ax = gca(fig);
heatmap(ax, M_matrix_up,[],[],[],'TickAngle', 45, 'Colorbar',true,'Colormap', 'jet',...
'MinColorValue', min(min(TRATIO)), 'MaxColorValue', max(max(TRATIO)));
set(ax,'YTick',1:m);
set(ax,'XTick',1:m);
set(ax,'XTickLabel',states,'Fontsize',10);
set(ax,'YTickLabel',states,'Fontsize',10);
title(ax, strcat('Map of Warming Dominance for the',{' '},char(quantiles(j))))
set(fig, 'PaperSize',[29.7 21.0], 'PaperPosition',[0 0 29.7 21.0])
print(fig, strcat('Heat_map_US_',char(quantiles(j)),'_1950_2021'),'-dpdf')
print(fig, strcat('Heat_map_US_',char(quantiles(j)),'_1950_2021'),'-dpng')
print(fig, strcat('Heat_map_US_',char(quantiles(j)),'_1950_2021'),'-deps')
Under the assumption that ifig is a figure number or figure handle instead of a figure name, this could be,
figure(ifig);
ax = gca(ifig);
heatmap(ax, M_matrix_up,[],[],[],'TickAngle', 45, 'Colorbar',true,'Colormap', 'jet',...
'MinColorValue', min(min(TRATIO)), 'MaxColorValue', max(max(TRATIO)));
set(ax,'YTick',1:m);
set(ax,'XTick',1:m);
set(ax,'XTickLabel',states,'Fontsize',10);
set(ax,'YTickLabel',states,'Fontsize',10);
title(ax, strcat('Map of Warming Dominance for the',{' '},char(quantiles(j))))
set(ifig, 'PaperSize',[29.7 21.0], 'PaperPosition',[0 0 29.7 21.0])
print(ifig, strcat('Heat_map_US_',char(quantiles(j)),'_1950_2021'),'-dpdf')
print(ifig, strcat('Heat_map_US_',char(quantiles(j)),'_1950_2021'),'-dpng')
print(ifig, strcat('Heat_map_US_',char(quantiles(j)),'_1950_2021'),'-deps')
Is there a warning message that appears when print() is not behaving expectedly?
Lola Gadea
Lola Gadea on 12 Nov 2024
Moved: Adam Danz on 12 Nov 2024
no warning message appears. The graph is created well in Matlab but then in pdf it appears empty. If it saves well in png
Please contact tech support and include instructions how to reproduce the problem. Feel free to include the URL of this thread. Also include your release information (R2024a).

Sign in to comment.

Answers (1)

To save a heatmap or any other graph plotted using MATLAB in PDF format, we need to ensure that the figure's size and position properties match the on-screen appearance. To achieve this, you can refer to my following code:
% 10x10 matrix with random values between 0 and 1
data = rand(10, 10);
% Create the heatmap
h = heatmap(data)
h.Title = 'Dummy Heatmap';
h.XLabel = 'X-axis Label';
h.YLabel = 'Y-axis Label';
colormap('jet');
colorbar;
% saving the figure in PDF format
set(gcf,'Units','inches');
screenposition = get(gcf,'Position');
set(gcf,...
'PaperPosition',[0 0 screenposition(3:4)],...
'PaperSize',[screenposition(3:4)]);
print -dpdf -painters myHeatMap
The above code firstly creates a heatmap as usual. Then, I set the figure's size and position and create the PDF accordingly to ensure that the figure appears the same as it does on the screen.
The code for saving the figure works as follows: The first two lines measure the size of your figure (in inches). The next line configures the print paper size to fit the figure size. The last line uses the “print” command to export a vector PDF document as the output.

1 Comment

Very useful. Thank you very much.
Although I don't understand why this didn't happen with previous versions of Matlab.

Sign in to comment.

Categories

Products

Release

R2024a

Tags

Asked:

on 10 Nov 2024

Commented:

on 3 Feb 2025

Community Treasure Hunt

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

Start Hunting!