Shift lables for x and y axis on a graph

1 回表示 (過去 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)
  1 件のコメント
Hamid
Hamid 2022 年 10 月 19 日
@Adam Danz Thank you so much.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLabels and Annotations についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by