필터 지우기
필터 지우기

How to create bar graph with categorical data

조회 수: 136 (최근 30일)
Gautam Ilango
Gautam Ilango 2017년 3월 15일
댓글: Toshia M 2023년 9월 20일
I would like to plot a bar graph separated in categories. I tried out the example code here from Matlab Documentation:
c = categorical({'apples','oranges','pears'});
prices = [1.23 0.99 2.3];
bar(c,prices)
But I don't get the categories displayed on the "x-Axis" of the graph, instead just int's 1-3.

채택된 답변

Gautam Ilango
Gautam Ilango 2017년 3월 18일
Meanwhile I have found this workaround:
prices = [1.23 0.99 2.3];
bar(prices)
set(gca,'xticklabel',{'apples','oranges','pears'});
  댓글 수: 4
Sajad Ahmad Rather
Sajad Ahmad Rather 2020년 10월 27일
Eactly. It just solved my problem in a second.
Thank you very much from the bottom of my heart.
Well done.
Toshia M
Toshia M 2023년 9월 20일
Starting in R2023b, you can specify x as a string vector (or as a cell array of character vectors). This enhancement makes it much easier to label the bars with text in a specific order. For example, create three bars with text labels:
c = ["pears" "apples" "oranges"];
prices = [1.23 0.99 2.3];
bar(c,prices)

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

추가 답변 (1개)

Chaman Dewangan
Chaman Dewangan 2021년 7월 13일
I found answer in mathworks.com as. Very happy it is working nicely.
One way to indicate categories for your bars is to specify X as a categorical array. The bar function uses a sorted list of the categories, so the bars might display in a different order than you expect. To preserve the order, call the reordercats function.
Define X as categorical array, and call the reordercats function to specify the order for the bars. Then define Y as a vector of bar heights and display the bar graph.
X = categorical({'Small','Medium','Large','Extra Large'});
X = reordercats(X,{'Small','Medium','Large','Extra Large'});
Y = [10 21 33 52];
bar(X,Y)

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by