Setting size of a figure

6 visualizaciones (últimos 30 días)
Ali Baig
Ali Baig el 30 de Abr. de 2025
Comentada: Adam Danz el 30 de Abr. de 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)

Respuestas (1)

Walter Roberson
Walter Roberson el 30 de Abr. de 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 comentario
Adam Danz
Adam Danz el 30 de Abr. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by