フィルターのクリア

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

1 回表示 (過去 30 日間)
Rahim Rahim
Rahim Rahim 2023 年 3 月 2 日
コメント済み: Voss 2023 年 3 月 3 日
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 Center および File ExchangeAnnotations についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by