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

8 ビュー (過去 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 Center および File ExchangeDiscrete Data Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by