Shift lables for x and y axis on a graph
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hamid
el 18 de Oct. de 2022
Comentada: Star Strider
el 19 de Oct. de 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)
0 comentarios
Respuesta aceptada
Star Strider
el 18 de Oct. de 2022
Editada: Star Strider
el 18 de Oct. de 2022
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 comentarios
Más respuestas (1)
Adam Danz
el 18 de Oct. de 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)
Ver también
Categorías
Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!