Data overlapping when plotting stacked bar graphs

조회 수: 11 (최근 30일)
niko1441
niko1441 2022년 4월 5일
편집: Kevin Holly 2022년 4월 6일
I am using Matlab version R2021b
This is the code I am using:
x = categorical({'Temp 1','Temp 2', 'Temp 3'});
x = reordercats(x, {'Temp 1', 'Temp 2', 'Temp 3'});
data_1 = [3e-12 4e-7 1.3e-8];
data_2 = [1.5e-6 6.43e-12 6.066e-12];
bar(x, [data_1; data_2], 0.5, "stack");
title("Total Data over Temperature")
xlabel('Temp');
ylabel('Data');
legend(["Data1", "Data2"],'location','northwest')
set(gca, 'YScale', 'log')
ylim([10e-14 10e-2])
This create the below graph:
If I swap the order:
bar(x, [data_2; data_1], 0.5, "stack");
Then I get the following graph:
Which leads me to think that the data is overlapping if you plot the smaller value second. I am not sure how to go about fixing this, without affecting how I am trying to show the data. Any ideas/suggestions would be appreciated!

답변 (2개)

Dave B
Dave B 2022년 4월 5일
편집: Dave B 2022년 4월 5일
The data aren't overlapping, you're simply seeing the effect of a log scale, which is a little strange for a stacked bar. The values control the height of the bars, and the values 6.43e-12 6.066e-12 are relatively large when they're near the bottom of the axes, but relatively small when further up.
It might be easier to interpret these data with a grouped rather than stacked bar?
x = categorical({'Temp 1','Temp 2', 'Temp 3'});
x = reordercats(x, {'Temp 1', 'Temp 2', 'Temp 3'});
data_1 = [3e-12 4e-7 1.3e-8];
data_2 = [1.5e-6 6.43e-12 6.066e-12];
bar(x, [data_1; data_2]);
title("Total Data over Temperature")
xlabel('Temp');
ylabel('Data');
legend(["Data1", "Data2"],'location','northwest')
set(gca, 'YScale', 'log')
ylim([10e-14 10e-2])

Kevin Holly
Kevin Holly 2022년 4월 5일
편집: Kevin Holly 2022년 4월 6일
You can adjust your lower y limit. See how changing it affects the plots below.
x = categorical({'Temp 1','Temp 2', 'Temp 3'});
x = reordercats(x, {'Temp 1', 'Temp 2', 'Temp 3'});
% Lower y limit = 0
figure
tiledlayout(2,4);
nexttile
data_1 = [3e-12 4e-7 1.3e-8];
data_2 = [1.5e-6 6.43e-12 6.066e-12];
bar(x, [data_1; data_2], 0.5, "stack");
title("0")
xlabel('Temp');
ylabel('Data');
set(gca, 'YScale', 'log')
ylim([0 10e-2])
% Lower y limit = 10e-8
nexttile
data_1 = [3e-12 4e-7 1.3e-8];
data_2 = [1.5e-6 6.43e-12 6.066e-12];
bar(x, [data_1; data_2], 0.5, "stack");
title("10e-8")
xlabel('Temp');
ylabel('Data');
set(gca, 'YScale', 'log')
ylim([10e-8 10e-2])
% Lower y limit = 10e-12
nexttile
data_1 = [3e-12 4e-7 1.3e-8];
data_2 = [1.5e-6 6.43e-12 6.066e-12];
bar(x, [data_1; data_2], 0.5, "stack");
title("10e-12")
xlabel('Temp');
ylabel('Data');
set(gca, 'YScale', 'log')
ylim([10e-12 10e-2])
% Lower y limit = 10e-14
nexttile
data_1 = [3e-12 4e-7 1.3e-8];
data_2 = [1.5e-6 6.43e-12 6.066e-12];
bar(x, [data_1; data_2], 0.5, "stack");
title("10e-14")
xlabel('Temp');
ylabel('Data');
set(gca, 'YScale', 'log')
ylim([10e-14 10e-2])
% Lower y limit = 0
nexttile
title("0")
xlabel('Temp');
ylabel('Data');
bar(x, [data_2; data_1], 0.5, "stack");
set(gca, 'YScale', 'log')
ylim([0 10e-2])
% Lower y limit = 10e-8
nexttile
title("10e-8")
xlabel('Temp');
ylabel('Data');
bar(x, [data_2; data_1], 0.5, "stack");
set(gca, 'YScale', 'log')
ylim([10e-8 10e-2])
% Lower y limit = 10e-12
nexttile
title("10e-12")
xlabel('Temp');
ylabel('Data');
bar(x, [data_2; data_1], 0.5, "stack");
set(gca, 'YScale', 'log')
ylim([10e-12 10e-2])
% Lower y limit = 10e-14
nexttile
title("10e-14")
xlabel('Temp');
ylabel('Data');
bar(x, [data_2; data_1], 0.5, "stack");
set(gca, 'YScale', 'log')
ylim([10e-14 10e-2])

카테고리

Help CenterFile Exchange에서 Use COM Objects in MATLAB에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by