如何在子图中用相同的颜色为所有第一个条着色,并用另一个颜色为第二个颜色着色?

时间:2021-04-21 04:13:10

标签: matlab plot progress-bar

我想创建一个子图,我有 6 个图形,在每个图形中我有 3 个条形,我想用相同的颜色为所有第一个条形着色并添加一个图例。请帮助我,这是代码:

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');

我想用一种颜色为所有第一个条着色,并添加一个如图所示的图例: enter image description here

请帮帮我。

1 个答案:

答案 0 :(得分:1)

您必须访问每个子图的 Bar 的 CData,如下所示: https://www.mathworks.com/help/matlab/ref/bar.html

示例: b1.CData(2,:) = [0.5 0 0.5]; % 带有 RGB 代码。