How can I edit x label so it doesn't overlap each other?

6 visualizaciones (últimos 30 días)
Mevlana Jalaluddin Rumi
Mevlana Jalaluddin Rumi el 22 de Dic. de 2020
Respondida: Aashray el 29 de Ag. de 2025 a las 9:14
This is my code at the moment,
figure(1)
bar(tabel_bus.jumlah_bus);
set(gca,'Xticklabel',tabel_bus.rute_tujuan)
grid on
xlabel('Rute Tujuan'); ylabel('Jumlah Bus');
title('Jumlah Bus Tiap Rute')
This is how my bar chart look,

Respuestas (1)

Aashray
Aashray el 29 de Ag. de 2025 a las 9:14
I understand that you are plotting a bar chart where the x-axis labels (route names) are quite long, and they currently overlap each other. This happens because by default MATLAB places labels horizontally, so when they are long and there are many of them, they cannot fit nicely.
A simple way to fix this is to rotate the labels using xtickangle.
Here is a small example with dummy data that shows the approach:
% Sample data
jumlah_bus = [3 7 12 5 9 11];
rute_tujuan = { ...
'BLOK M - CILEDUG', ...
'CAWANG - PGC', ...
'PLUMPANG - LINCING', ...
'PERINTIS - PANTENG', ...
'SUNTER - PRUMO', ...
'DUREN KOSAMBI - KAMAL'};
% Create figure
figure
bar(jumlah_bus);
% Apply labels
set(gca,'XTickLabel',rute_tujuan)
% Rotate to avoid overlap
xtickangle(45);
xlabel('Rute Tujuan');
ylabel('Jumlah Bus');
title('Jumlah Bus Tiap Rute');
grid on
This will show each route on two stacked lines, making the axis less cluttered.
Here are documentation links of the functions I used for reference:

Categorías

Más información sobre Axis Labels en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by