How can I add a print the first bar with a color, second with color different and the last with a different color in subplot ?

2 vues (au cours des 30 derniers jours)
I have this program
x1=[1;2;3]
y1 = [91,25,3];
y2 = [71,22,13];
y3 = [81,22,33];
y4 = [71,12,63];
y5 = [61,42,23];
y6 = [51,21,23];
figure;
subplot(3,2,1);
b1=bar(x1,y1);
ylabel('Cost per Byte (%)');
ylim([0 max(y1)+10]);
title('Aaa');
subplot(3,2,2);
b2=bar(x1,y2);
ylabel('Security (%)');
ylim([0 max(y2)+10]);
title('Second plot');
subplot(3,2,3);
b3=bar(x1,y3);
ylabel('Data rate (kbps)');
ylim([0 max(y3)+10]);
title('Third plot');
subplot(3,2,4);
b4=bar(x1,y4);
ylabel('Delay (ms)');
ylim([0 max(y4)+10]);
title('Fourth plot');
subplot(3,2,5);
b5=bar(x1,y5);
ylabel('Jitter (ms)');
ylim([0 max(y5)+10]);
title('Fifth plot');
subplot(3,2,6);
b6=bar(x1,y6);
ylabel('Loss ratio 10^6 (ms)');
ylim([0 max(y6)+10]);
title('Sixth plot');
legend('peaks');
set(b1,'FaceColor','red');
I want colorate all the first bars by a color and add a legend like the picture :
Please help me.

Réponse acceptée

Star Strider
Star Strider le 21 Avr 2021
The only way that I am aware of to plot the individual bar colours is to use a loop:
x1 = [1;2;3]
x1 = 3×1
1 2 3
y1 = [91,25,3];
cm = [1 0 0; 1 1 0; 0 1 1];
figure
hold on
for k = 1:numel(x1)
b1(k) = bar(x1(k),y1(k), 'FaceColor',cm(k,:));
end
hold off
and so for the rest. It would be worthwhile to make a function out of this if you are going to use it repeatedly, for example:
x1 = [1;2;3];
y1 = [91,25,3];
y2 = [71,22,13];
y3 = [81,22,33];
y4 = [71,12,63];
y5 = [61,42,23];
y6 = [51,21,23];
figure;
subplot(3,2,1);
b1=barfcn(x1,y1);
ylabel('Cost per Byte (%)');
ylim([0 max(y1)+10]);
title('Aaa');
subplot(3,2,2);
b2=barfcn(x1,y2);
ylabel('Security (%)');
ylim([0 max(y2)+10]);
title('Second plot');
subplot(3,2,3);
b3=barfcn(x1,y3);
ylabel('Data rate (kbps)');
ylim([0 max(y3)+10]);
title('Third plot');
subplot(3,2,4);
b4=barfcn(x1,y4);
ylabel('Delay (ms)');
ylim([0 max(y4)+10]);
title('Fourth plot');
subplot(3,2,5);
b5=barfcn(x1,y5);
ylabel('Jitter (ms)');
ylim([0 max(y5)+10]);
title('Fifth plot');
subplot(3,2,6);
b6=barfcn(x1,y6);
ylabel('Loss ratio 10^6 (ms)');
ylim([0 max(y6)+10]);
title('Sixth plot');
legend('peaks');
set(b1,'FaceColor','red');
function bh = barfcn(x,y)
cm = [1 0 0; 1 1 0; 0 1 1];
hold on
for k = 1:numel(x)
bh(k) = bar(x(k),y(k));
bh(k).FaceColor = cm(k,:);
end
xlim([min(x)-0.5 max(x)+0.5])
hold off
end
Experiment to get the result you want.
  3 commentaires
Star Strider
Star Strider le 21 Avr 2021
My pleasure!
Change the ‘cm’ matrix to whatever you want. The colours are defined by its rows, not columns, so experiment with those to get the colours you want. The the documentation on colormap for a choice of several different options, and specific colours.
For example,
cm = turbo(3);
would be one option.
Rahim Rahim
Rahim Rahim le 23 Avr 2021
@Star Strider It is done.
Can you help me please ? https://www.mathworks.com/matlabcentral/answers/811000-how-can-i-add-a-legend-in-the-end-of-sublot-on-the-bottom-of-sublot?s_tid=prof_contriblnk

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by