Bar graphs don't group bars if there is only one group in the data

15 ビュー (過去 30 日間)
FM
FM 2022 年 12 月 1 日
コメント済み: VBBV 2022 年 12 月 2 日
I have a lot of bar graphs to plot. The bars must be grouped into pairs. If the number of pairs is one, then grouping doesn't seem to work:
% Artificial data: Two vertical vectors of height 3
x=reshape(randperm(6),3,2)
% Correct grouping into pairs
subplot(1,2,1)
barh(x);
% NO grouping :(
subplot(1,2,2)
barh(x(1,:));
Is there any way to force barh to group consistently? I want each column of "x" to form a series, even if it is a series of 1. So x(1,:) forms two series, only 1 bar each. In the grouped bar chart, it should form one group of two bars, each bar with a different colour (just like each group on the left subplot). In contrast, the right subplot shows that it forms a single series of two bars (or a single series of two groups, each containing one bar). They also have separate tick lables, which bars in the same group should not.
I am using Matlab 2022a.
  1 件のコメント
VBBV
VBBV 2022 年 12 月 2 日
I have moved the code in comment to main body of the answer now.

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

採用された回答

VBBV
VBBV 2022 年 12 月 1 日
移動済み: VBBV 2022 年 12 月 1 日
like this ?
% Artificial data: Two vertical vectors of height 3
x=reshape(randperm(6),3,2)
x = 3×2
5 6 2 3 1 4
% Correct grouping into pairs
subplot(1,2,1)
barh(x);
subplot(1,2,2)
barh(1,x(1,:).') % like this
  3 件のコメント
FM
FM 2022 年 12 月 1 日
移動済み: VBBV 2022 年 12 月 1 日
Thanks for replying, VBBV.
My apologies, but I think I may not have been clear. I want each column of "x" to form a series, even if it is a series of 1. So x(1,:) forms two series, only 1 bar each. In the grouped bar chart, it should form one group of two bars.
FM
FM 2022 年 12 月 1 日
移動済み: VBBV 2022 年 12 月 1 日
Yes! After seeing your syntax, I searched for where this is described in the doc page for "barh". I found it under the description of the "y" argument -- not where I would expect it.
I'd like to mark your response as "the answer" to my question. Currently, the code that addresses the question is in the comment, whereas the main body of the answer contains different code. Is there any way to put your latest example code into the main body of the answer posting?
Thanks!

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

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by