How to label names in bar graph?

29 ビュー (過去 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 Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by