在Matlab轮廓栏中使用不同颜色的条

时间:2020-10-10 07:06:31

标签: matlab plot bar-chart

我正在使用Matlab R2020b。

为什么条形轮廓线(黑色的)很宽使用循环更改每个条形的颜色

col(1,:) = [1 0 0]; % -- red
col(2,:) = [0 1 0]; % -- green
col(3,:) = [0 0 1]; % -- blue
col(4,:) = [1 1 0]; % -- yellow
figure
b(1) = bar(dt3(1),p3(1),'FaceColor',col(1,:),'BarWidth', 1, 'LineWidth',0.001);
hold on;

for i=2:length(dt3)
    if cid3(i) ~= cid3(i-1)
        if c == 4
            c = 1;
        else
            c = c + 1;
        end
        
        b(i) = bar(dt3(i),p3(i),'FaceColor',col(c,:),'BarWidth', 1, 'LineWidth',0.001);
        txt{k} = num2str(cid3(i));
        lh(k) = b(i);
        k  = k + 1;
    else
        b(i) = bar(dt3(i),p3(i),'FaceColor',col(c,:),'BarWidth', 1, 'LineWidth',0.001);
    end
end

enter image description here

您可以看到,由于黑条几乎更宽,因此很难正确看到颜色。

但是,如果我直接绘制相同的条形图而不更改颜色,则它们会更细(蓝色)

bar(dt3,p3);

enter image description here

深蓝色的条较细。

1 个答案:

答案 0 :(得分:1)

已解决。

只需更改第一个代码(带有循环的代码)中的杠线并删除边缘线:

b(i) = bar(dt3(i),p3(i),'FaceColor',col(c,:),'BarWidth', 1, 'LineWidth',0.001);

b(i) = bar(dt3(i),p3(i),'FaceColor',col(c,:),'BarWidth', 0.8, 'EdgeColor','none');

其中BarWidth = 0.8是默认的条宽大小。

col(1,:) = [1 0 0]; % -- red
col(2,:) = [0 1 0]; % -- green
col(3,:) = [0 0 1]; % -- blue
col(4,:) = [1 1 0]; % -- yellow
figure
b(1) = bar(dt3(1),p3(1),'FaceColor',col(1,:),'BarWidth', 0.8, 'EdgeColor','none');
hold on;

for i=2:length(dt3)
    if cid3(i) ~= cid3(i-1)
        if c == 4
            c = 1;
        else
            c = c + 1;
        end
        
        b(i) = bar(dt3(i),p3(i),'FaceColor',col(c,:),'BarWidth', 0.8, 'EdgeColor','none');
        txt{k} = num2str(cid3(i));
        lh(k) = b(i);
        k  = k + 1;
    else
        b(i) = bar(dt3(i),p3(i),'FaceColor',col(c,:),'BarWidth', 0.8, 'EdgeColor','none');
    end
end

我仍然必须更改颜色,它们看起来非常有光泽。

enter image description here