如何使用MATLAB代码绘制具有不同颜色的每个条形图?

时间:2020-08-17 11:42:02

标签: matlab

我在MATLAB中获得了条形图的8种颜色代码,但我还需要更多。怎么可能?

附加图像中的第一个条获得两种颜色,为什么会发生这种情况,请告知我用一种颜色进行填充。

MatLab代码:

ei_with_plvei = categorical({'[14]', '[31]', '[36]', '[45]', '[08]', '[09]', '[10]', '[11]', '[13]', 'test1', 'test2'});
x_axis = ei_with_plvei; 
y_axis = [67.47 78.04 50.81 57.58 79.53 80.53 70.43 74.73 77.81 95 97];
figure, 
bar(x_axis, y_axis);
hold on;
bar(x_axis(1),y_axis(1));
set(bar(1),'FaceColor','b');
hold on;
bar(x_axis(2),y_axis(2));
set(bar(2),'FaceColor','r');
hold on;
bar(x_axis(3),y_axis(3));
set(bar(3),'FaceColor','g');
hold on;
bar(x_axis(4),y_axis(4));
set(bar(4),'FaceColor','c');
hold on;
bar(x_axis(5),y_axis(5));
set(bar(5),'FaceColor','m');
hold on;
bar(x_axis(6),y_axis(6));
set(bar(6),'FaceColor','y');
hold on;
bar(x_axis(7),y_axis(7));
set(bar(7),'FaceColor','k');
hold on;
bar(x_axis(8),y_axis(8));
set(bar(8),'FaceColor','w');
hold on;
bar(x_axis(9),y_axis(9));
set(bar(9),'FaceColor','m');
hold on;
bar(x_axis(10),y_axis(10));
set(bar(10),'FaceColor','m');
hold on;
bar(x_axis(11),y_axis(11));
set(bar(11),'FaceColor','m');

输出:enter image description here

1 个答案:

答案 0 :(得分:0)

由于set(bar(1),....),第一个小节得到两个小节。这将返回高度为1的新创建的条的句柄。set(bar(2),....)返回高度为2的新创建的条的句柄,等等。

此外,只需调用一次hold on就足够了。然后进行设置。

关于颜色,您可以使用

  • RGBbar(x_axis(11),y_axis(11),'FaceColor',[1 1 0.5]);
  • Hexadecimalbar(x_axis(10),y_axis(10),'FaceColor','#FF60F0');

工作代码示例:

ei_with_plvei = categorical({'[14]', '[31]', '[36]', '[45]', '[08]', '[09]', '[10]', '[11]', '[13]', 'test1', 'test2'});
    x_axis = ei_with_plvei; 
    y_axis = [67.47 78.04 50.81 57.58 79.53 80.53 70.43 74.73 77.81 95 97];
    figure, 
    bar(x_axis, y_axis);
    hold on;
    bar(x_axis(1),y_axis(1),'FaceColor','b');
    bar(x_axis(2),y_axis(2),'FaceColor','r');
    bar(x_axis(3),y_axis(3),'FaceColor','g');
    bar(x_axis(4),y_axis(4),'FaceColor','c');
    bar(x_axis(5),y_axis(5),'FaceColor','m');
    bar(x_axis(6),y_axis(6),'FaceColor','y');
    bar(x_axis(7),y_axis(7),'FaceColor','k');
    bar(x_axis(8),y_axis(8),'FaceColor','w');
    bar(x_axis(9),y_axis(9),'FaceColor','m');
    bar(x_axis(10),y_axis(10),'FaceColor','#FF60F0');
    bar(x_axis(11),y_axis(11),'FaceColor',[1 1 0.5]);