Shift lables for x and y axis on a graph

1 Ansicht (letzte 30 Tage)
Hamid
Hamid am 18 Okt. 2022
Kommentiert: Star Strider am 19 Okt. 2022
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)

Akzeptierte Antwort

Star Strider
Star Strider am 18 Okt. 2022
Bearbeitet: Star Strider am 18 Okt. 2022
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 Kommentare
Hamid
Hamid am 19 Okt. 2022
@Star Strider Thanks a lot for your kind response.
Star Strider
Star Strider am 19 Okt. 2022
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Adam Danz
Adam Danz am 18 Okt. 2022
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)

Kategorien

Mehr zu Labels and Annotations finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by