Changing the position of a plot

조회 수: 52 (최근 30일)
L'O.G.
L'O.G. 2023년 1월 23일
댓글: L'O.G. 2023년 1월 23일
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.

채택된 답변

Walter Roberson
Walter Roberson 2023년 1월 23일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by