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

6 次查看(过去 30 天)
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,

回答(1 个)

Aashray
Aashray 2025-8-29,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:

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by