在条形图中具有相同颜色的组,在条形中具有图例元素

时间:2019-01-04 22:11:57

标签: matlab bar-chart figure

我有一个如下所示的条形图,x是一个8x3的矩阵。我的密码是

x=[0.2193   0.2281  0;
0.193   0.1404  0;
0.2045  0.1875  0.159;
0.0625  0.0568  0;
0.1993  0.1554  0.1318;
0.0878  0.0034  0;
0.1369  0.1103  0.1027;
0.0951  0.076   0];

x0=10;
y0=10;
width=1200;
height=500;
set(gcf,'position',[x0,y0,width,height])

a=bar(x,'BarWidth',0.9);
a(1).FaceColor=[0.9290, 0.6940, 0.1250];
a(2).FaceColor=[0, 0.4470, 0.7410];
a(3).FaceColor=[0.4660, 0.6740, 0.1880];


lgd=legend('Method 1','Method 2','Method 3');
title('False Negative Rates')
xlabel('Clusters')
ylabel('False Negative Rate')
xticklabels({'Cl1 - PRed','Cl1 - PYellow','Cl2 - PRed','Cl2 - PYellow', 'Cl3 - PRed','Cl3 - PYellow', 'Cl4 - PRed','Cl4 - PYellow'})
saveas(gcf,'False Negatives.png')

您可以看到图表中有8个组。 enter image description here 我需要对这张图做三件事:

  1. 第一,第三,第五和第七组应被涂成红色,其他则应涂成黄色。
  2. 我需要在每个栏中写入图例元素。如果它不适合放入内部,则以垂直方式放在其上方。红条将具有白色字体,黄条将具有黑色字体。
  3. 如果该值为0,则在水平轴上用一条线显示。

我经历了这个,但是不能类似地适用。 How to make groups of horizontal bars have the same color?

我如何实现所有这些或全部?

1 个答案:

答案 0 :(得分:2)

您需要遵循以下算法,才能根据要求正确获取图形。

  1. 为x矩阵中的每个元素分别循环。
  2. 然后为每个元素绘制相应的条形图并保持不变。
  3. 检查开关箱中的每个列号,并相应地将图例垂直放置在条形上方。
  4. 接下来,检查行号是否为奇数,如果是,则将条形颜色更改为红色,将图例颜色更改为白色。在此步骤中,根据条的长度确定图例在条内/外的位置。
  5. 如果行是偶数,则将条形颜色更改为黄色,将图例颜色更改为黑色,并根据条形长度相应地固定图例位置。
  6. 最后,检查x条的长度是否为0,如果是,则增加“线宽”并向上滑动条的图例。
  7. 增加条的差距变量并关闭内部循环。
  8. 再次增加条的差距变量并关闭外循环。
  9. 推迟,然后放置标题,刻度和标签。

下面提供了代码。

clc
close all

% declare the points, for the bars
x=[0.2193   0.2281  0;
0.193   0.1404  0;
0.2045  0.1875  0.159;
0.0625  0.0568  0;
0.1993  0.1554  0.1318;
0.0878  0.0034  0;
0.1369  0.1103  0.1027;
0.0951  0.076   0];

% declare the figure width and height
x0=10;
y0=10;
width=1200;
height=500;
% create the figure
set(gcf,'position',[x0,y0,width,height]);
% get the number of rows and column in x points
[row, col] = size(x);
% variable to decide the gap between the bars
p = 1;

%%%%%%  Loop for Rows %%%%%%%%
for r = 1:row

    %%%%%%%%%% loop for each elements in each row %%%%%%%
    % loop for columns %
    for c = 1:col
        % draw the individual bar
        a = bar(p, x(r,c), 'BarWidth',0.9); hold on
        % switch the column value
        switch(c)
            % if column is 1 then create bar legend as "Method 1"
            % vertically just above the bar
            case 1 
                t1 = text(p,x(r,c),'Method 1','rotation',90);
            % if column is 2 then create bar legend as "Method 2"
            % vertically just above the bar
            case 2
                t1 = text(p,x(r,c),'Method 2','rotation',90);
            % if column is 3 then create bar legend as "Method 3"
            % vertically just above the bar
            case 3
                t1 = text(p,x(r,c),'Method 3','rotation',90);
        end

        % Now check whether the row is odd
        % if yes then change the bar color to Red
        if(rem(r, 2) ~= 0)
            set(a, 'FaceColor', 'r')
            % Change the legend color to white in red bars
            t1.Color = 'White';
            % check if x value is equals or greaten than 
            % 0.05, it means the legend fit inside the bars
            if(x(r,c) >= 0.05)
                % slide down the legend into the bars
                t1.Position = [p, x(r,c)-0.04];
            else
                % else change the color of the 
                % legend to black and keep the
                % position unchanged, because if the 
                % position remains outside the red bars
                % then the legend would not be visible due 
                % to the color white and hence change it to black
                t1.Color = 'black';
            end
        else
           % else, it means row is even then
           % change the bar color to yellow
           set(a, 'FaceColor', 'y')
           % if bar color is yellow then change the legend
           % color to black
            t1.Color = 'black';
            % check if length of bar is greater than 0.05
            % if yes slide the legend position inside the bar
              if(x(r,c) >= 0.05)
                t1.Position = [p, x(r,c)-0.04];
              end
        end

        % finally, check if x is equal to 0
        % then make the line width of bar to
        % 2 points, to make it more visible
        % and slide up the legend above the bar
        % to few fractional points to make it more ligible
        if(x(r,c) == 0)
            a.LineWidth = 2;
            t1.Position = [p, x(r,c)+0.002];
        end

        % increment the legend gap as y-axis
        p = p+1;

    end
    %%%%% END the Column loop %%%%%%%%%

    % on each row end increment the gap of bars to
    % make it categorized as each row.
    p = p+1;
end
%%%%%%%%% END the Row loop %%%%%%%%%%%

hold off
% set the title
title('False Negative Rates')
% set the labels
xlabel('Clusters')
ylabel('False Negative Rate')
% mark the xticks to put the xticklables
% in next line
xticks([2:4:32])
% change the xtick labels at corresponding xticks
xticklabels({'Cl1 - PRed','Cl1 - PYellow','Cl2 - PRed','Cl2 - PYellow', 'Cl3 - PRed','Cl3 - PYellow', 'Cl4 - PRed','Cl4 - PYellow'})
saveas(gcf,'False Negatives.png')

输出

enter image description here