How to change x-axis ticks labels in stackedplot?

20 visualizaciones (últimos 30 días)
Ammy
Ammy el 6 de Abr. de 2022
Editada: Voss el 27 de Jun. de 2024
h = stackedplot(rand(6,3));
I want to set x-axis ticks according to my own defined set i.e., instead of 1:6, I want to replace x-axisticks [1, 2,3 ,4,5,6] to ['A', 'S','T', 'AAA', 'BBB', 'ZZZ'] , by rotating it to 90 degree that is vertically insted of horizontally?

Respuesta aceptada

Voss
Voss el 10 de Abr. de 2022
There does not seem to be an easy way to set the XTick or XTickLabel of a StackedLineChart object (such as what's created by stackedplot):
data = rand(6,3);
h = stackedplot(1:6,data);
% try a couple of things, neither of which work
try
set(h,'XTick',1:6,'XTickLabel',{'A' 'S' 'T' 'AAA' 'BBB' 'ZZZ'})
catch ME
disp(['Cannot use set(_,''XTick''): ' ME.message]);
try
xticks(h,1:6)
catch ME
disp(['Cannot use xticks(_): ' ME.message]);
end
end
Cannot use set(_,'XTick'): Unrecognized property XTick for class StackedLineChart.
Cannot use xticks(_): Using xticks with stackedplot is not supported.
However, you can do it if you use tiledlayout (or subplot):
figure();
tiledlayout(3,1,'TileSpacing','tight');
for i = [1 2 3]
ax = nexttile;
plot(data(:,i));
box off
if i < 3
set(get(ax,'XAxis'),'Visible','off');
end
ylabel(ax,sprintf('Column %d',i), ...
'Rotation',0, ...
'HorizontalAlignment','right');
end
% now ax is the bottom axes
set(ax, ...
'XTick',1:6, ...
'XTickLabel',{'A','S','T','AAA','BBB','ZZZ'}, ...
'XTickLabelRotation',90);
  2 comentarios
Ammy
Ammy el 11 de Abr. de 2022
Editada: Voss el 27 de Jun. de 2024
@Voss Thank you!
Voss
Voss el 11 de Abr. de 2022
@Ammy You're welcome!

Iniciar sesión para comentar.

Más respuestas (2)

Simon Allosserie
Simon Allosserie el 6 de Abr. de 2022
Editada: Simon Allosserie el 6 de Abr. de 2022
Use xtick and xticklabels to change to the A, S, T, ... labels
I don't understand what you further mean with the rotating 90° though.
  1 comentario
ikushou
ikushou el 3 de Nov. de 2022
No you can not use it when you use stackedplot.
that's really annoying

Iniciar sesión para comentar.


Paul Carchipulla-Morales
Paul Carchipulla-Morales el 8 de Feb. de 2023
There is a quicker method that worked for me using NodeChildren in MATLAB2021b.
h = stackedplot(rand(6,3));
ax = findobj(h.NodeChildren, 'Type','Axes');
set(ax,'XTick',[1:6],'XTickLabel',{'A', 'S','T', 'AAA', 'BBB', 'ZZZ'})

Categorías

Más información sobre Line 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!

Translated by