Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
Rahim Rahim
Rahim Rahim am 2 Mär. 2023
Kommentiert: Voss am 3 Mär. 2023
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

Antworten (1)

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

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by