When saving a MATLAB figure with arrows and text as a PDF using print, the annotations are rescaled incorrectly, but using exportgraphics preserves the correct scaling

18 次查看(过去 30 天)
Hi everyone,
I'm encountering an issue when saving a figure in PDF format using MATLAB.
Here's the context: I create a figure and plot some data using the following code (Part from the code comes from https://www.youtube.com/watch?v=wP3jjk1O18A&t=383s&ab_channel=PhysicsLaure):
hfig = figure; % save the figure handle in a variable
stairs(Results2Coal.Power_demand,'LineWidth',1.5,'DisplayName','Load','Color',colorsScheme(13,:));
hold on
stairs(Results2Coal.P_generated_coal_1,'LineWidth',1.5,'DisplayName','G1','Color',colorsScheme(2,:));
stairs(Results2Coal.P_generated_coal_2,'LineWidth',1.5,'DisplayName','G2','Color',colorsScheme(9,:));
plot(Results2Coal.P_max_coal_2*ones(1,24),'LineWidth',0.5,'HandleVisibility','off','Color','k','LineStyle','--');
plot(Results2Coal.P_max_coal_1*ones(1,24),'LineWidth',0.5,'HandleVisibility','off','Color','k','LineStyle','--');
xlabel('time $t$ (h)')
ylabel('Power (MW)')
legend('Location', 'northwest','FontSize',12)
picturewidth = 20; % set this parameter and keep it forever
hw_ratio = 0.65; % feel free to play with this ratio
set(findall(hfig,'-property','FontSize'),'FontSize',12) % adjust fontsize to your document
axis([0 25 1000 8000])
set(gca,'YTick',[1000,1500, 2000,2685, 4000,5000,6000,7000, 8000]) % impose sensible tickmark locations
set(gca,'YTickLabel',{'1000', '$G1_{min}$','2000', '$G1_{max}$','4000','$G2_{max}$','6000','7000','8000'}) % put in informative labels at these tickmarks
set(gca,'XTick',[0, 4,8,12, 16,20, 24]) % impose sensible tickmark locations
set(gca,'XTickLabel',{'00:00', '04:00', '08:00','12:00', '16:00', '20:00', '24:00'}) %consistent tick interval
set(findall(hfig,'-property','Box'),'Box','off') % optional
set(findall(hfig,'-property','Interpreter'),'Interpreter','latex')
set(findall(hfig,'-property','TickLabelInterpreter'),'TickLabelInterpreter','latex')
set(hfig,'Units','centimeters','Position',[3 3 picturewidth hw_ratio*picturewidth])
pos = get(hfig,'Position');
set(hfig,'PaperPositionMode','Auto','PaperUnits','centimeters','PaperSize',[pos(3), pos(4)])
fname = ['CommitmentProblem with two power plants', datestr(now,'dd yyyy hhMM')];
print(hfig,fname,'-dpdf','-vector','-fillpage')
The problem arises when I use the figure menu to insert arrows and text (via Insert/Arrows and Insert/Text). These arrows and text appear correctly in the MATLAB figure window, but when I save the figure as a PDF, the arrows are rescaled and don't match the original placement.
If I save the figure as a .png file, the scaling is correct, and the arrows and text maintain their placement. However, I prefer to use the .pdf format for my figures.
When I use:
exportgraphics(hfig, [fname, '.pdf'], 'ContentType', 'vector');
This method works and preserves the correct scaling of arrows and text in the PDF.
Although this solves my problem, I am still curious to understand why the print function does not work as expected.
Has anyone experienced a similar problem or have any insights on why print causes this scaling issue while exportgraphics does not?
Thanks in advance for your help!

回答(1 个)

Divyanshu
Divyanshu 2024-6-27,5:52
Hi Georgi,
I think there are some issues expected when a image is saved using '-vector' option with 'print' function. The following documentation link provides more information on this:
Hope it helps!

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by