Shift lables for x and y axis on a graph

조회 수: 7 (최근 30일)
Hamid
Hamid 2022년 10월 18일
댓글: Star Strider 2022년 10월 19일
Dear experts,
I have a graph as attached an plotted using pcolor. The values in x-axis and y-axis are from 0 to 30 and I wanted to change them from -15 to 15. I tried XTick but the output is not correct. Please find the graphs attached. Thank you in advance.
lab=-15:1:15;
xticks(lab)

채택된 답변

Star Strider
Star Strider 2022년 10월 18일
편집: Star Strider 2022년 10월 18일
Perhaps xticklabels (although it takes a bit of experimenting to get it to work) —
figure
pcolor(rand(30))
xtl = xticklabels;
xticks(linspace(1, 30, numel(xtl)+1))
xticklabels(compose('%3d',linspace(-15, 15, numel(xtl)+1)))
yticks(linspace(1, 30, numel(xtl)+1))
yticklabels(compose('%3d',linspace(-15, 15, numel(xtl)+1)))
% set(gca, 'XTick',linspace(1, 30, numel(xtl)+1), 'XTickLabel',compose('%3d',linspace(-15, 15, numel(xtl)+1)))
% set(gca, 'YTick',linspace(1, 30, numel(xtl)+1), 'YTickLabel',compose('%3d',linspace(-15, 15, numel(xtl)+1)))
Both of these approaches work, and produce the same result.
EDIT — (18 Oct 2022 at 13:26)
Initially forgot about the y-axis. Same approach, different functions.
EDIT — (18 Oct 2022 at 15:12)
Actually, a better option would be to use surf and then the view function to look at the surface from the top —
x = 0:30;
y = 0:30;
[X,Y] = ndgrid(x,y);
Z = exp(-((X-15).^2+(Y-15).^2)*0.025);
figure
surf(X,Y,Z)
colormap(turbo)
xt = xticks;
xticklabels(xt-15)
yt = yticks;
yticklabels(yt-15)
figure
surf(X,Y,Z)
colormap(turbo)
xt = xticks;
xticklabels(xt-15)
yt = yticks;
yticklabels(yt-15)
view(0,90)
.
  댓글 수: 2
Hamid
Hamid 2022년 10월 19일
@Star Strider Thanks a lot for your kind response.
Star Strider
Star Strider 2022년 10월 19일
As always, my pleasure!

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

추가 답변 (1개)

Adam Danz
Adam Danz 2022년 10월 18일
Your best bet is to specify the x and y values in pcolor so you don't have to alter the ticks in the first place.
Example:
x = -15:15;
y = 0:30;
z = rand(31,31);
pcolor(x,y,z)

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by