How to label names in bar graph?
45 次查看(过去 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
0 个评论
采纳的回答
Star Strider
2023-10-29
编辑:Star Strider
2023-10-29
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 个评论
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
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!