Clear Filters
Clear Filters

How to set the plot box to particular size

77 views (last 30 days)
Ole
Ole on 24 Sep 2021
Answered: Jaswanth on 26 Jul 2024
How to set the plot box to pixel size.
x = linspace(1,20,100);
y = x.^2;
figure(1)
plot(x,y)
xlabel('X')
ylabel('Y')
The code below will set the figure size. Instead I would like only the plot box without labels to have the size of 200x500.
x0=10;
y0=10;
width = 500;
height = 200;
set(gcf,'position',[x0,y0,width,height])

Answers (1)

Jaswanth
Jaswanth on 26 Jul 2024
Hi,
To set the plot box to a specific pixel size without changing the overall figure size, you can manipulate the Position property of the axes within the figure.
Please refer to the following example code showing how you can achieve a plot box size of 200x500 pixels:
% Add first part of the code you have provided
% Set the figure size
x0 = 10;
y0 = 10;
figure_width = 600; % Adjusted to accommodate the desired plot box size and labels
figure_height = 300; % Adjusted to accommodate the desired plot box size and labels
set(gcf, 'Position', [x0, y0, figure_width, figure_height])
% Set the plot box size
plot_box_width = 500;
plot_box_height = 200;
set(gca, 'Units', 'pixels', 'Position', [50, 50, plot_box_width, plot_box_height])
In the modified code,
  • The figure size is set to 600x300 pixels to provide enough space for the plot box and the labels.
  • The Position property of the axes (gca) is set to [50, 50, 500, 200] pixels. The first two values [50, 50] set the position of the bottom-left corner of the plot box within the figure window, and the last two values [500, 200] set the width and height of the plot box.
Kindlly make sure to adjust the figure size and the position of the plot box as needed to ensure that all elements (including labels) are properly displayed.
I hope the information provided above is helpful.

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!