# How do you set the x axis ticks / labels to be equal to the y axis ones?

조회 수: 101 (최근 30일)
L'O.G. 2022년 4월 6일
댓글: Star Strider 2022년 4월 6일
I thought I could do this by the following, but it's not working:
ax1 = gca;
set(ax1,'XTick',get(ax1,'YTick'));
Instead, the x axis still has different ticks (more ticks, same range). How do I make the ticks / labels the same for both axes?
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Les Beckham 2022년 4월 6일
I think this should work if the range of the data is compatible, though it may not look very good.
Can you provide an example where this doesn't work?
Example where it does what you tell it but it may not be what you want.
plot(linspace(0,5,10), 2*rand(1,10))
ax1 = gca;
set(ax1,'XTick',get(ax1,'YTick'));
grid on

댓글을 달려면 로그인하십시오.

### 채택된 답변

Star Strider 2022년 4월 6일
The tick ranges are governed by the original data.
If the tick ranges are different, the only options are to either set the x-tick labels to be compatible, or re-scale the y-values to be equal to the x-values. Getting the exact values on both axes is only possible if the original number of tick values on each axis are the same —
x = 1:10;
y = x.^2/2;
figure
plot(x, y)
grid
figure
plot(x, y)
grid
yt = get(gca, 'YTick');
xt = get(gca, 'XTick');
xtl = linspace(min(yt), max(yt), numel(xt));
set(gca,'XTick',xt,'XTickLabel',compose('%.0f',xtl))
Here, there are 10 x-ticks and 11 y-ticks, so the new x-tick lables reflect that compromise.
.
##### 댓글 수: 2없음 표시없음 숨기기
L'O.G. 2022년 4월 6일
Thanks! The issue ended up being I was setting the ticks before changing the font size! D'oh!
Star Strider 2022년 4월 6일
If the tick ranges are the same (same number of ticks on both axes), then try this:
set(gca, 'XTick',xt, 'XTickLabel',yt)
x = 1:10;
y = x.^2/2.25; % Now: Equal Numbers Of Ticks On Both Axes
figure
plot(x, y)
grid
figure
plot(x, y)
grid
yt = get(gca, 'YTick');
xt = get(gca, 'XTick');
set(gca, 'XTick',xt, 'XTickLabel',yt)
.

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Image Analyst 2022년 4월 6일
Try one of these (not both):
axis equal
axis square

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by