필터 지우기
필터 지우기

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 CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by