【问题标题】:Having groups of the same color in a bar graph and having the legend elements in the bars在条形图中具有相同颜色的组并在条形图中具有图例元素
【发布时间】:2019-05-31 11:48:22
【问题描述】:

我有一个如下所示的条形图,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 个组。 我需要用这张图做三件事:

  1. 第 1 组、第 3 组、第 5 组和第 7 组应为红色,其他组应为黄色。
  2. 我需要在每个栏内写入图例元素。如果它不适合内部,则以垂直方式在其上方。红色条为白色字体,黄色条为黑色字体。
  3. 如果值为 0,则在水平轴上用一条线显示。

我经历了这个,但无法类似地申请。 How to make groups of horizontal bars have the same color?

我怎样才能实现所有这些?

【问题讨论】:

    标签: matlab bar-chart figure


    【解决方案1】:

    您需要按照以下算法正确获取图表。

    1. 为 x 矩阵中的每个元素单独循环。
    2. 为每个元素绘制相应的条形图并按住。
    3. 检查 switch-case 中的每个列号,并相应地将图例垂直放置在条形上方。
    4. 接下来,检查行号是否为奇数,如果是,则将条形颜色更改为红色,将图例颜色更改为白色。在此步骤中,根据条形长度固定条形内部/外部的图例位置。
    5. 如果 row 是偶数,则将条形颜色更改为黄色,将图例颜色更改为黑色,并根据条形长度相应地固定图例位置。
    6. 最后检查x条长度是否为0,如果是则增加Line-Width并向上滑动条的图例。
    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')
    

    输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多