Data overlapping when plotting stacked bar graphs

30 views (last 30 days)
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!

Answers (2)

Dave B
Dave B on 5 Apr 2022
Edited: Dave B on 5 Apr 2022
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 on 5 Apr 2022
Edited: Kevin Holly on 6 Apr 2022
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])

Community Treasure Hunt

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

Start Hunting!

Translated by