How to create bar plot with groups x-axis labels

11 ビュー (過去 30 日間)
Amir-Homayoun
Amir-Homayoun 2020 年 7 月 8 日
コメント済み: Adam Danz 2020 年 7 月 8 日
Hello,
I would like to create a bar plot with the labels in the x-axis grouped. This is what I have created in Excel. As you can see, the x-axis is grouped into
  • 90-min and 1-week, then
  • Theta and Gamma, then
  • Sham and Active
This grouping can be very helpful to avoid long labels. Thanks.

採用された回答

Alan Stevens
Alan Stevens 2020 年 7 月 8 日
Here's one way (there are almost certainly slicker ways!):
% Arbitrary data
x = 0:10:70;
y = [ 5 2 3 7 8 1 7 4];
% Labels
L1 = ["Sham" "Active"];
L2 = ["Theta" "Gamma"];
L3 = ["90-min" "1-week"];
% Positions for text
yp1 = -0.5*ones(size(x));
yp2 = 2*yp1;
yp3 = -1.5;
% Bar chart
bar(x,y)
set(gca,'Position',[0.13 0.2 0.775 0.775]); % Make space for labels
set(gca,'XTickLabel','') % Remove numbers
% Insert text labels
for i = 1:2:length(x)
text(x(i)-4, yp1(i), L1(1))
text(x(i+1)-4, yp1(i), L1(2))
end
for i = 1:4:length(x)
text(x(i), yp2(i), L2(1))
text(x(i+2), yp2(i), L2(2))
end
text(x(2), yp3, L3(1))
text(x(6), yp3, L3(2))
This resuts in:
  2 件のコメント
Amir-Homayoun
Amir-Homayoun 2020 年 7 月 8 日
Fantastic. Thanks Alan. Very helpful.
Adam Danz
Adam Danz 2020 年 7 月 8 日
+1, nicely done.
Also see the 'grouped' style for bar plots.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by