Create grouped bar chart using figure handle

5 ビュー (過去 30 日間)
Christian Carstensen
Christian Carstensen 2018 年 9 月 23 日
Hello mad labbers
I'm having trouble creating a grouped bar chart using figure handles.
It ends up being stacked instead of grouped when adding data to it using its handle. The two cases below illustrates the problem - A gives the intended plot, but the plot in B is stacked instead of grouped.
Does anyone if one can get plot A using a handle as in B?
x = [1 2 3];
y1 = [3 6 8];
y2 = [6 5 3];
Case A:
y = [y1; y2]';
ax = subplot(1, 2, 1);
hold(ax, 'on');
bar(ax, x, y, 'grouped')
xlabel('A')
Case B:
ax = subplot(1, 2, 2);
hold(ax, 'on')
bar(ax, x, y1, 'grouped');
bar(ax, x, y2, 'grouped');
xlabel('B')

採用された回答

jonas
jonas 2018 年 9 月 23 日
編集済み: jonas 2018 年 9 月 23 日
I don't know what you mean by figure handles. However, when you call the bar function mutliple times the outputs are independent of one another and thus become stacked like in your example. What you can do to avoid this is to pad the data with nans.
ax = subplot(1, 2, 2);
hold(ax, 'on')
bar(ax, x, [y1;nan(size(y1))]', 'grouped');
bar(ax, x, [nan(size(y2));y2]', 'grouped');
xlabel('B')
  1 件のコメント
Christian Carstensen
Christian Carstensen 2018 年 9 月 23 日
Thank you, Jonas! Perhaps I was being a little uncareful with the wording, but I simply meant the ax object when I referred to 'figure handles'. The plural was unnecessary.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by