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

1 次查看(过去 30 天)
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

回答(1 个)

Voss
Voss 2023-3-2
编辑:Voss 2023-3-2
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 个评论
Voss
Voss 2023-3-2
@Rahim Rahim You're welcome, I've updated the answer to account for the updated question.
Voss
Voss 2023-3-3
@Rahim Rahim: Does this work for you? If so, please Accept This Answer. Otherwise, let me know if you have any questions.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by