Clear Filters
Clear Filters

How change the color of text bar like the color of the bar ?

1 view (last 30 days)
I have the following figure
I want to change the color of text like the color of its bar, for example 100 => blue, 83.3 => red ...etc
Also I want to text appear Vertical not Horezental.
This is the code:
for k1 = 1:4
ctr(k1,:) = bsxfun(@plus, Bar(k1).XData, Bar(k1).XOffset'); % Note: ‘XOffset’ Is An Undocumented Feature, This Selects The ‘bar’ Centres
ydt(k1,:) = Bar(k1).YData; % Individual Bar Heights
end
ctr
% ydt
for k1 = 1:size(ctr,2)
format short
text(ctr(:,k1), ydt(:,k1), sprintfc('%.1f',ydt(:,k1)), 'HorizontalAlignment','center', 'VerticalAlignment','bottom')
end

Answers (1)

Voss
Voss on 2 Mar 2023
Edited: Voss on 2 Mar 2023
data = rand(4,4);
data(:,[3 4]) = 0;
Bar = bar(data);
[m,n] = size(data);
ctr = zeros(n,m);
ydt = zeros(n,m);
for k1 = 1:n
ctr(k1,:) = bsxfun(@plus, Bar(k1).XData, Bar(k1).XOffset'); % Note: ;XOffset’ Is An Undocumented Feature, This Selects The ‘bar’ Centres
ydt(k1,:) = Bar(k1).YData; % Individual Bar Heights
end
ctr
ctr = 4×4
0.7273 1.7273 2.7273 3.7273 0.9091 1.9091 2.9091 3.9091 1.0909 2.0909 3.0909 4.0909 1.2727 2.2727 3.2727 4.2727
% ydt
for k1 = 1:n
for k2 = 1:m
% format short
text(ctr(k1,k2), ydt(k1,k2), sprintfc('%.1f',ydt(k1,k2)), ...
'Color',Bar(k1).FaceColor, ...
'Rotation',90, ...
'HorizontalAlignment','left', ...
'VerticalAlignment','middle')
end
end
  3 Comments
Voss
Voss on 2 Mar 2023
@Rahim Rahim You're welcome, I've updated the answer to account for the updated question.
Voss
Voss on 3 Mar 2023
@Rahim Rahim: Does this work for you? If so, please Accept This Answer. Otherwise, let me know if you have any questions.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!