% clc
% clear all
x = categorical({'Filament'; 'Model'});
%y = [0.415047 0.11398; 0.41697 0.013588];
y = [0.415047; 0.41697];
z = [0.11398,0.013588];
bar (x,y,'grouped')
hold on
yyaxis right
%ylim([0.35, 0.45]);
ylabel('Weight (g)');
%err = [0.002542, 0.002192];
%errorbar(A, y, err, '- .', 'MarkerSize', 8)
yyaxis left
%ylim([0, 0.15]);
ylabel('Mass reduction (%)');
%err = [0.003711,0.001355];
%errorbar(A, z, err, '- .', 'MarkerSize', 8)
bar (x,z,'grouped')
hold on
Trying to get a bar graph that share same axis and have two y axis. Tried 'grouped' function but still come out as a stacked bar graph.

1 件のコメント

Adam Danz
Adam Danz 2021 年 12 月 6 日
I've formatted your code and produced the resultant figure using the Run feature. What would you like to change about this figure?

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

 採用された回答

Dave B
Dave B 2021 年 12 月 6 日
編集済み: Dave B 2021 年 12 月 6 日

2 投票

bar graphs don't collaborate between sides of a yyaxis very well, but you can fake it by padding with zeros:
x = categorical({'Filament'; 'Model'});
y = [0.415047; 0.41697];
z = [0.11398; 0.013588];
nil = [0; 0];
bar(x, [y nil], 'grouped')
ylabel('Mass reduction (%)');
yyaxis right
bar(x, [nil z], 'grouped')
ylabel('Weight (g)');

9 件のコメント

Adam Danz
Adam Danz 2021 年 12 月 6 日
+1
x = categorical({'Filament'; 'Model'});
y = [0.415047; 0.41697];
z = [0.11398; 0.013588];
nil = [0; 0];
yyaxis left % <--- adding this maintains color pairing between bars and axes
bar(x, [y nil], 'grouped')
ylabel('Mass reduction (%)');
yyaxis right
bar(x, [nil z], 'grouped')
ylabel('Weight (g)');
Dave B
Dave B 2021 年 12 月 6 日
編集済み: Dave B 2021 年 12 月 6 日
Oops, I was so excited I forgot to include left :)
Also worth considering is using nan(2,1) instead of [0; 0] which will make your code resiliant to changing the 'BaseValue' property, though mixing that with yyaxis would make for a confusing chart:
x = categorical({'Filament'; 'Model'});
y = [0.415047; 0.41697];
z = [0.11398; 0.013588];
nil = nan(2, 1);
yyaxis left
bar(x, [y nil], 'grouped')
ylabel('Mass reduction (%)');
yyaxis right
bar(x, [nil z], 'grouped')
ylabel('Weight (g)');
Adam Danz
Adam Danz 2021 年 12 月 7 日
or even,
bar(x, [y, nan(size(y))], 'grouped')
bar(x, [nan(size(z)), z], 'grouped')
Shu-An Hsieh
Shu-An Hsieh 2021 年 12 月 10 日
Thank you for the assist! I am trying to put an error bar on the firgure. However, it keeps show in the middle of two bar. Can you let me know how I can fix it? Thanks ahead!
Adam Danz
Adam Danz 2021 年 12 月 10 日
Dave B
Dave B 2021 年 12 月 10 日
編集済み: Dave B 2021 年 12 月 10 日
@Adam Danz - we added a property called XEndPoints to Bar in 2019b to give you an easier way to get at the locations of the bars...would that be useful for the answer you noted above? (We also added a YEndPoints that's more useful for stacked bars)
Here's some code that uses XEndPoints and makes sure to grab the correct (visible) bars to get XEndPoints from...(and adds some personal preferences for errorbar aesthetics):
x = categorical({'Filament'; 'Model'});
y = [0.415047; 0.41697];
z = [0.11398; 0.013588];
ysig=[.1;.2];
zsig=[.02;.005];
nil = nan(2, 1);
yyaxis left
hold on
b=bar(x, [y nil], 'grouped');
errorbar(b(1).XEndPoints, y, ysig, 'LineStyle', 'none', 'Marker', 'none', ...
'LineWidth', 1, 'Color', 'k', 'CapSize',0);
ylabel('Mass reduction (%)');
yyaxis right
% you don't really need b= here, because the first bar has the same
% XEndPoints:
b=bar(x, [nil z], 'grouped');
errorbar(b(2).XEndPoints, z, zsig, 'LineStyle', 'none', 'Marker', 'none', ...
'LineWidth', 1, 'Color', 'k', 'CapSize',0);
ylabel('Weight (g)');
Dave B
Dave B 2021 年 12 月 10 日
編集済み: Dave B 2021 年 12 月 10 日
Additional note:
I find this visualization a little confusing, it highlights the comparison of Mass reduction vs Weight for Filament, and then again for Model, but that comparison feels wrong given the different y axis scales.
I'd consider redesigining to have the two Mass reduction bars together and the two weight Comparisons together, and then dropping the yyaxis bit:
x = categorical({'Filament'; 'Model'});
y = [0.415047; 0.41697];
z = [0.11398; 0.013588];
ysig=[.1;.2];
zsig=[.02;.005];
tiledlayout(1,2)
nexttile
bar(x,y)
hold on
errorbar(x,y,ysig, 'LineStyle', 'none', 'Marker', 'none', ...
'LineWidth', 1, 'Color', 'k', 'CapSize',0);
ylabel('Mass Reduction (%)')
box off
nexttile
bar(x,z,'SeriesIndex',2)
hold on
errorbar(x,z,zsig, 'LineStyle', 'none', 'Marker', 'none', ...
'LineWidth', 1, 'Color', 'k', 'CapSize',0);
ylabel('Weight (g)')
box off
Shu-An Hsieh
Shu-An Hsieh 2021 年 12 月 10 日
Thank you so much for the help this is really helpful!
Adam Danz
Adam Danz 2021 年 12 月 10 日
Thanks for the reminder, Dave. I couldn't remember if I had used those newer properties or not but it looks like I did.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

タグ

質問済み:

2021 年 12 月 6 日

コメント済み:

2021 年 12 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by