Changing the position of a plot

188 views (last 30 days)
L'O.G.
L'O.G. on 23 Jan 2023
Commented: L'O.G. on 23 Jan 2023
How do you move everything in a figure (plot, axes, axis labels) up while preserving the aspect ratio of the plot? It seems I can do this by setting the OuterPosition, specifically the first two entries, but I don't understand how they work, or how I can figure out what the current values are.

Accepted Answer

Walter Roberson
Walter Roberson on 23 Jan 2023
data1 = rand(1,20);
data2 = rand(1,20);
figure()
ax1 = subplot(2,1,1);
plot(data1)
ax2 = subplot(2,1,2)
ax2 =
Axes with properties: XLim: [0 1] YLim: [0 1] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.3412] Units: 'normalized' Show all properties
plot(data2);
figure()
ax1 = subplot(2,1,1);
plot(data1)
ax2 = subplot(2,1,2)
ax2 =
Axes with properties: XLim: [0 1] YLim: [0 1] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.3412] Units: 'normalized' Show all properties
plot(data2);
ax2.Units
ans = 'normalized'
ax2.OuterPosition
ans = 1×4
0 0.0655 1.0000 0.4049
ax2.OuterPosition(2) = ax2.OuterPosition(2)*2;
Notice how the bottom plot is closer to the upper plot in the second figure.
OuterPosition uses the standard MATLAB conventions: it is a vector of four values, where the first value is the x coordinate of the bottom of the position; the second value is the y coordinate of the left of the position; the third coordinate is the width; the fourth coordinate is the height.
The default (at least for this use) is "normalized" units -- units that are a fraction of the size of the container. In this particular example the container is a figure(), but if you had happened to use uipanel() then the normalized coordinates would be relative to the containing panel.
It is common to switch the Units of an axes to Pixels; if that is done before changing the OuterPosition, then the OuterPosition would report back in that new units and adjustments would be relative to that new Units.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!