Problems printing graphics in pdf format
Show older comments
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
Lola Gadea
on 10 Nov 2024
Moved: DGM
on 10 Nov 2024
Walter Roberson
on 10 Nov 2024
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')
Adam Danz
on 11 Nov 2024
Is there a warning message that appears when print() is not behaving expectedly?
Lola Gadea
on 12 Nov 2024
Moved: Adam Danz
on 12 Nov 2024
Adam Danz
on 12 Nov 2024
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).
Answers (1)
Vedant Shah
on 31 Jan 2025
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
Lola Gadea
on 3 Feb 2025
Categories
Find more on Data Distribution Plots 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!