Shift lables for x and y axis on a graph

7 views (last 30 days)
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)

Accepted Answer

Star Strider
Star Strider on 18 Oct 2022
Edited: Star Strider on 18 Oct 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)
.

More Answers (1)

Adam Danz
Adam Danz on 18 Oct 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)

Categories

Find more on Labels and Annotations in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!