Setting size of a figure

8 visualizzazioni (ultimi 30 giorni)
Ali Baig
Ali Baig il 30 Apr 2025
Commentato: Adam Danz il 30 Apr 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)

Risposte (1)

Walter Roberson
Walter Roberson il 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 Commento
Adam Danz
Adam Danz il 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.

Accedi per commentare.

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Prodotti


Release

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by