How can I title my bar graph in x-axis?

Hey,
As you know the default for x-axis in the Bar graph is just numbers. Could you please give me a hint how I can replace them by names? e.g. I would like to have a bar graph with the names of months (Jan, Feb, Mar, ...)in the x-axis.
Thank you,
Mehdi

 채택된 답변

Grzegorz Knor
Grzegorz Knor 2011년 9월 30일

11 개 추천

I have no idea why. This code should work:
figure
name = {'m1';'m2';'m3';'m4';'m5'};
x = [1:5]; y = [19,24,42,12,14];
bar(x,y)
set(gca,'xticklabel',name)

댓글 수: 2

Joelmir
Joelmir 2017년 5월 25일
Very good!! thanks
Philippe Corner
Philippe Corner 2018년 1월 26일
Hi Grzegorz Knor, how could i name the xbars in a sequence.. for example to have numbers from 1:1:10.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Grzegorz Knor
Grzegorz Knor 2011년 9월 30일

2 개 추천

bar(1:12)
set(gca,'xticklabel',{'a','b','c','d','e','f','g','h','i','j','k','l'})
M G
M G 2011년 9월 30일

0 개 추천

Thank you Grzegorz, At the moment I have a cell array containing names:
fname = {'m1','m2','m3','m4','m5'};
and when I name them in the x-axis as you mentioned:
x = [1:5]; y = [19,24,42,12,14]; name = [fanme];
bar(x,y)
set(gca,'xticktlabel',name)
each of columns are named as m1m2m3m4m5
How can I have m1 for column1, m2 for column2 and so on?

댓글 수: 1

Your problem can be solved like this:
fname = {'m1','m2','m3','m4','m5'};
x = [1:5]; y = [19,24,42,12,14];
bar(x,y)
set(gca, 'XTick', 1:length(fname),'XTickLabel',fname);

댓글을 달려면 로그인하십시오.

Ckenne
Ckenne 2026년 5월 4일 20:11

0 개 추천

dt = 0.01; % Time step for numerical approximation tx = 0 : dt : 10; th = 3 : dt : 12; x = cos(pi * tx / 2); h = sin(pi * th); % Perform convolution and scale by dt y = conv(x, h) * dt; % Generate the time axis for the output ty = tx(1) + th(1) : dt : tx(end) + th(end); % Plotting figure; subplot(3,1,1); plot(tx, x); title('x(t)'); grid on; subplot(3,1,2); plot(th, h); title('h(t)'); grid on; subplot(3,1,3); plot(ty, y); title('Convolution y(t) = x(t) * h(t)'); xlabel('Time (t)'); ylabel('y(t)'); grid on;

카테고리

도움말 센터File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

태그

질문:

M G
2011년 9월 30일

답변:

2026년 5월 4일 20:11

Community Treasure Hunt

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

Start Hunting!

Translated by