필터 지우기
필터 지우기

How to label names in bar graph?

조회 수: 34 (최근 30일)
OMAR MOUSA
OMAR MOUSA 2023년 10월 29일
댓글: the cyclist 2023년 10월 30일
I faced a problem in labeling names which gave me errors all the time. the labels include one name and several numbers. the code runs only with number labels but the name cannot be included in the labels
I wrote the code down and got the figure how can I fix this and change the number 1 with "THD" ?
I used categorical, but the order of labels has showed randomly
dataA = {DBTGA, DBT1A, DBML1A, DBML2A, CPACA, EMSB1A, SSBMFA, AggA};
dataB = {DBTGB, DBT1B, DBML1B, DBML2B, CPACB, EMSB1B, SSBMFB, AggB};
dataC = {DBTGC, DBT1C, DBML1C, DBML2C, CPACC, EMSB1C, SSBMFC, AggC};
titles = {'DB TG','DB T1','DB ML 1','DB ML 2','CP AC','EMSB 1','SSB MF','Aggregate'};
xlables = ["THD" 3 5 7 9 11 13 15]; % when i put 1 rather THD it works but for "THD" doesn't work
for i = 1:numel(dataA)
dataA{i}=mean(dataA{i});
dataB{i}=mean(dataB{i});
dataC{i}=mean(dataC{i});
end
for i = 1:numel(dataA)
subplot(3,3,i)
bar(xlables,[dataA{i};dataB{i};dataC{i}].')
xlabel({'Harmonic Order'},'FontSize', 10)
ylabel({'% of fundimental'},'FontSize', 10)
title(titles{i},'FontSize', 10)
grid, grid minor
end

채택된 답변

Star Strider
Star Strider 2023년 10월 29일
편집: Star Strider 2023년 10월 29일
One approach (using xticklabels introduced in R2016b, as was string) —
Data = rand(8,3);
xlables = ["THD" 3 5 7 9 11 13 15]; % when i put 1 rather THD it works but for "THD" doesn't work
figure
bar(Data)
xticklabels(xlables)
EDIT — (29 Oct 2023 at 21:50)
Added reference to string and xticklabels being introduced in the same release. Content otherwise unchanged.
.
  댓글 수: 3
Star Strider
Star Strider 2023년 10월 30일
As always, my pleasure!
the cyclist
the cyclist 2023년 10월 30일
In newer versions of MATLAB, you can do the simpler
Data = rand(8,3);
xlables = ["THD" 3 5 7 9 11 13 15];
bar(xlables,Data)
Note that part of the reason this works is that MATLAB converts the numeric values in xlables into strings, so the variable is a string array [which can be used directly in bar()].

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

추가 답변 (1개)

the cyclist
the cyclist 2023년 10월 29일
Can you upload the data, so that we can run your code? You can use the paper clip icon in the INSERT section of the toolbar.
What version of MATLAB are you using? Certain data types cannot be used as x-axis values in older releases.
I expect this will work if you use this version of the syntax for bar:
bar([dataA{i};dataB{i};dataC{i}].') % Using the default x-axis values
set(gca,"XTickLabels",xlables)
This method uses your vector only to label the ticks, not as the values of the ticks.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by