How to change the transparancy (FaceAlpha) of single bars in a bar plot

2 views (last 30 days)
Hi,
I have a a code that plots data from a 14x4 array in a bar graph, I want to access entire groups of 4 bars (for example CDC19) so that I can change their transparancy.
b = bar(str2double(Out_Glycolysis_Fold(2:end,2:end)));
hold on
plot(xlim,[1 1], 'k')
hold off
xticklabels(Out_Glycolysis_Fold(2:end,1));
ylabel('Fold Change', 'FontSize', 13);
xlabel('Genes', 'FontSize', 13);
ax = gca;
ax.XTickLabelRotation = 45;
legend('Mid Exponential', 'Late Exponential', 'Early Diauxic', 'Mid Diauxic', 'Mid Stationary');
set(ax, 'FontSize', 15);
title(title_Glyc);
ylim([0.3 1.7])
ax = gca;
ob = ax.Children;
set(ob(2), 'FaceAlpha', 0.1
Only when I try to access for example the second group from the object it changes the transparancy of all the yellow bars, does anyone happen to know how to solve this?

Answers (1)

Ameer Hamza
Ameer Hamza on 24 Sep 2020
Edited: Ameer Hamza on 24 Sep 2020
It seems from the documentation that changing individual bars like this isn't currently supported. The workaround is to create each bar as a separate object. For example, something like this
M = rand(3, 4);
alphas = [0.2 0.1 0.5 0.8;
1 0.9 0.8 0.7;
0.1 0.3 0.7 1]; % alpha value for all bars
f = figure();
ax = axes();
hold(ax);
for i = 1:size(M, 1)
for j = 1:size(M, 2)
bar(i + interp1([1 size(M, 2)], [-0.3 0.3], j), M(i, j), ...
'BarWidth', 0.15, ...
'FaceAlpha', alphas(i, j))
end
end

Community Treasure Hunt

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

Start Hunting!