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

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 日

0 投票

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:

カテゴリ

ヘルプ センター および File Exchange で Line Plots についてさらに検索

製品

リリース

R2019a

質問済み:

2020 年 12 月 22 日

回答済み:

2025 年 8 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by