Setting size of a figure

9 vues (au cours des 30 derniers jours)
Ali Baig
Ali Baig le 30 Avr 2025
Commenté : Adam Danz le 30 Avr 2025
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)

Réponses (1)

Walter Roberson
Walter Roberson le 30 Avr 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 commentaire
Adam Danz
Adam Danz le 30 Avr 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by