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

1 vue (au cours des 30 derniers jours)
Haythem Zouabi
Haythem Zouabi le 2 Oct 2019
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?

Réponse acceptée

Jess Lovering
Jess Lovering le 2 Oct 2019
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 commentaire
Haythem Zouabi
Haythem Zouabi le 3 Oct 2019
Modifié(e) : Haythem Zouabi le 3 Oct 2019
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,

Connectez-vous pour commenter.

Plus de réponses (1)

Jess Lovering
Jess Lovering le 3 Oct 2019
Modifié(e) : Jess Lovering le 3 Oct 2019
You can add this line at the end and see if that works:
xticklabels(x);

Catégories

En savoir plus sur Dialog Boxes dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by