How to change color bar? how to keep initial abscisses values?

조회 수: 2 (최근 30일)
Haythem Zouabi
Haythem Zouabi 2019년 10월 2일
편집: Jess Lovering 2019년 10월 3일
This is my code before adding a command to display values at the tip of the bars.
X = categorical({'1024','512','256','128'});
X = reordercats(X,{'1024','512','256','128'});
x=[1024,512,256,128];
Pitch= 17*10^(-3)
tai15 = []
for i = 1:length(x)
T = (x(i)*Pitch)/15
tai15=[tai15, T]
end
tai20=[]
for i = 1:length(x)
T = (x(i)*Pitch)/20
tai20=[tai20, T]
end
tai30=[]
for i = 1:length(x)
T = (x(i)*Pitch)/30
tai30=[tai30, T]
end
Tail=[tai15; tai20; tai30]
y=Tail'
b = bar(X, y)
b(1).FaceColor = [1 0 0]
b(2).FaceColor = [0 1 0]
b(3).FaceColor = [0 0 1]
legend({'X15','X20','X30'})
set(gcf,'color','w') % pour les bordures.
set(gca, 'FontSize', 9, 'linewidth', 1, 'FontName','Times New Roman'); %la taille des éléments sur les axes.
xlabel('Spatial resolution (pixels)','FontSize',12, 'FontName','Times New Roman')
ylabel('scene size (mm²)','FontSize',12, 'FontName','Times New Roman')
When i add the command to display values at the tip of the bars, bar color changes? and abscisses changes also? I want to keep the initial color and abscisses?
X = categorical({'1024','512','256','128'});
X = reordercats(X,{'1024','512','256','128'});
x=[1024,512,256,128];
Pitch= 17*10^(-3)
tai15 = []
for i = 1:length(x)
T = (x(i)*Pitch)/15
tai15=[tai15, T]
end
tai20=[]
for i = 1:length(x)
T = (x(i)*Pitch)/20
tai20=[tai20, T]
end
tai30=[]
for i = 1:length(x)
T = (x(i)*Pitch)/30
tai30=[tai30, T]
end
Tail=[tai15; tai20; tai30]
y=Tail'
b = bar(X, y)
b(1).FaceColor = [1 0 0]
b(2).FaceColor = [0 1 0]
b(3).FaceColor = [0 0 1]
hBar = bar(y,1);
for k1 = 1:size(y,2)
ctr(k1,:) = bsxfun(@plus, hBar(1).XData, hBar(k1).XOffset'); % Note: ‘XOffset’ Is An Undocumented Feature, This Selects The ‘bar’ Centres
ydt(k1,:) = hBar(k1).YData; % Individual Bar Heights
text(ctr(k1,:), ydt(k1,:), sprintfc('%.2f', ydt(k1,:)), 'HorizontalAlignment','center', 'VerticalAlignment','bottom', 'FontSize',8, 'Color','b')
end
legend({'X15','X20','X30'})
set(gcf,'color','w') % pour les bordures.
set(gca, 'FontSize', 9, 'linewidth', 1, 'FontName','Times New Roman'); %la taille des éléments sur les axes.
xlabel('Spatial resolution (pixels)','FontSize',12, 'FontName','Times New Roman')
ylabel('scene size (mm²)','FontSize',12, 'FontName','Times New Roman')
Do you have solution for this problem? i want to keep the initial bar color and abscisses?

채택된 답변

Jess Lovering
Jess Lovering 2019년 10월 2일
To make sure I understand your intention - you want the colors to stay red, green, and blue and not default to the standard colormap colors, correct? When you add the line: hBar = bar(y,1) after you define the colors of the bars it deletes the original bars that you created. Then you make the bars again and then do not assign a color. See the below code for a possible fix.
X = categorical({'1024','512','256','128'});
X = reordercats(X,{'1024','512','256','128'});
x=[1024,512,256,128];
Pitch= 17*10^(-3)
tai15 = []
for i = 1:length(x)
T = (x(i)*Pitch)/15
tai15=[tai15, T]
end
tai20=[]
for i = 1:length(x)
T = (x(i)*Pitch)/20
tai20=[tai20, T]
end
tai30=[]
for i = 1:length(x)
T = (x(i)*Pitch)/30
tai30=[tai30, T]
end
Tail=[tai15; tai20; tai30]
y=Tail'
% b = bar(X, y)
% b(1).FaceColor = [1 0 0]
% b(2).FaceColor = [0 1 0]
% b(3).FaceColor = [0 0 1]
hBar = bar(y,1);
hBar(1).FaceColor = [1 0 0]
hBar(2).FaceColor = [0 1 0]
hBar(3).FaceColor = [0 0 1]
for k1 = 1:size(y,2)
ctr(k1,:) = bsxfun(@plus, hBar(1).XData, hBar(k1).XOffset'); % Note: ‘XOffset’ Is An Undocumented Feature, This Selects The ‘bar’ Centres
ydt(k1,:) = hBar(k1).YData; % Individual Bar Heights
text(ctr(k1,:), ydt(k1,:), sprintfc('%.2f', ydt(k1,:)), 'HorizontalAlignment','center', 'VerticalAlignment','bottom', 'FontSize',8, 'Color','b')
end
legend({'X15','X20','X30'})
set(gcf,'color','w') % pour les bordures.
set(gca, 'FontSize', 9, 'linewidth', 1, 'FontName','Times New Roman'); %la taille des éléments sur les axes.
xlabel('Spatial resolution (pixels)','FontSize',12, 'FontName','Times New Roman')
ylabel('scene size (mm²)','FontSize',12, 'FontName','Times New Roman')
  댓글 수: 1
Haythem Zouabi
Haythem Zouabi 2019년 10월 3일
편집: Haythem Zouabi 2019년 10월 3일
Thank you.
I want to know also how to keep the abscisses values introduced in the begenning.
Abscisses values must be {1024, 512, 256, 128} but i got 1, 2, 3, 4?
Best regards,

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jess Lovering
Jess Lovering 2019년 10월 3일
편집: Jess Lovering 2019년 10월 3일
You can add this line at the end and see if that works:
xticklabels(x);

카테고리

Help CenterFile Exchange에서 GNSS Positioning에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by