Setting size of a figure
26 views (last 30 days)
Show older comments
Hi!
I am trying to plot a graph twice: once with axes shown and once without axes. However, this results in two figures with different sizes. I want the two images to have exactly same size (in terms of pixels) and scale, so that when I copy them in to, say MS Word, I can excatly overlap them. I will appreciate your help.
Following is my Matlab script:
clear all
close all
clc
x0 = 10;
y0 = 10;
width = 1000;
height = 1000;
%%%%%%%%%%%%%%%%%%%%%%%%%
x = 0 : 0.001 : 100;
y = sin(x);
%%%%%%%%%%%%%%%%%%%%%%%%%
figure(1)
plot(x, y);
xlabel('\bfAngular Frequency (Hz)'), ylabel('\bfStorage Modulus (MPa)')
xlim([0 101]), ylim([-1.5 1.5])
set(gcf,'position',[x0, y0, width, height])
exportgraphics(gca, 'With Axis.png', 'Resolution', 600)
%%%%%%%%%%%%%%%%%%%%%%%%%
figure(2)
plot(x, y);
xlabel('\bfAngular Frequency (Hz)'), ylabel('\bfStorage Modulus (MPa)')
xlim([0 101]), ylim([-1.5 1.5])
set(gcf,'position',[x0, y0, width, height])
axis off
exportgraphics(gca, 'Without Axis.png', 'Resolution', 600)
0 Comments
Answers (1)
Walter Roberson
on 30 Apr 2025
You are not exporting the figures: you are exporting the axes, and the axes are different size. The one without the axes does not need to leave room for the labels and ticks.
See the axes property 'InnerPosition'
1 Comment
Adam Danz
on 30 Apr 2025
+1
There are several additional name-value arguments in exportgraphics that are currently (R2024b) only available in MATLAB Online but will soon (25a-prerelease) be broadly available. These new arguments help control the output size and include Width, Height, Units, Padding, and PreserveAspectRatio.
See Also
Categories
Find more on 2-D and 3-D 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!