【问题标题】:Stacked bar from Table in matlabmatlab中表格的堆积条
【发布时间】:2017-02-11 17:23:23
【问题描述】:

我想从表格创建堆积条形图。这里有一个我正在查看的表类型的 MWE:

clear all; 

country1=rand(5,1); 
 country2=rand(5,1);
 country3=rand(5,1);
 country4=rand(5,1);
 country5=rand(5,1);

 date=1990:1994;
 T=table(date',country1,country2,country3,country4,country5);
 T.Properties.VariableNames{1}='date';
 T.Total=sum(T{:,2:end},2); 
 T{:,2:end} = T{:,2:end}./T.Total; 
 A = table2array(T);
 A(:,[1,end])=[];
 A=sort(A,2); 
 TT=array2table(A,'VariableNames',{'country1','country2','country3','country4','country5'});
TT.Date=T.date;
TT.Total=T.Total;
T_new=table(TT.Date, TT.country1,TT.country2,TT.country3,TT.country4,TT.country5,TT.Total);
T_new.Properties.VariableNames=T.Properties.VariableNames;
T_new.World=sum(T{:,2:4},2);
T_new.World=1-(T_new.country4+T_new.country5); 
T_new(:,[2:4,end-1])=[];

T_new

date    country4    country5     World 
    ____    ________    ________    _______

    1990     0.2933     0.29471     0.41199
    1991    0.31453     0.34511     0.34035
    1992    0.22595     0.29099     0.48307
    1993    0.26357     0.33336     0.40306
    1994    0.28401     0.28922     0.42677

堆叠条形图的类型

=====================

基于T_new 表,我想创建一个堆积条形图。在“x”轴上,图表应显示日期(1990、1991 等),并且每个日期应为 一个 堆叠条形。因此,例如,对于1990,应该有一个条形图堆叠值0.2933 0.29471 0.41199

理想情况下,我还想在堆栈栏中包含 (country1, country2, world) 的标签以获取对应值。

我如何在 matlab 中做到这一点?

【问题讨论】:

    标签: matlab matlab-figure stacked-chart matlab-table


    【解决方案1】:

    您可以执行以下操作:

    bar(T_new{:,1},T_new{:,2:end},'stacked')
    legend(T_new.Properties.VariableNames(2:end))
    

    【讨论】:

      【解决方案2】:

      您提供的代码在以下行包含错误:

      T{:,2:end} = T{:,2:end}./T.Total
      
      Error using  ./ 
      Matrix dimensions must agree.
      Error in stacked_bars (line 14)
      T{:,2:end} = T{:,2:end}./T.Total;
      

      因为T{:,2:end}(5 x 6) 矩阵,而T.Total(5 x 1) 数组

      您可以将其替换为该行,例如:

      T{:,2:end}=bsxfun(@rdivide,T{:,2:end},T.Total)
      

      一旦修复了错误,另一种绘制标签的方法(相对于已发布的答案)可能是使用text function 在每个stackedbars 中绘制一个字符串。

      您可以通过这种方式识别要绘制字符串的点的xy 坐标:

      • x:对于每组条,是对应的date(您需要将该值向左移动一点,以便使文本相对于条居中,因为text 使用x 坐标作为起点
      • y:对于第一个标签(较低的)可能只是条高的一半;从第二个栏开始,您需要添加前一个栏的高度

      这种方法的可能实现如下:

      % Get the T_new data
      x=table2array(T_new)
      x=x(:,2:end)
      % Ientify the number of bars
      n_s_bars=size(x,2)
      % Open a Figure for the plot
      figure(123)
      % Plot the stacked bars
      bar(T_new{:,1},T_new{:,2:end},'stacked')
      % Get the names of the table variables
      v_names=T_new.Properties.VariableNames
      
      % Loop over the dates
      for i=1:length(date)
         % Create the label string:
         %   country_x (or world)
         %   percentage
         str=sprintf('%s\n%f',v_names{2},x(i,1))
         % Print the label in the center of the first bar
         tx=text(date(i)-.3,x(i,1)/2,str,'Color',[1 1 1])
         % Loop over the bars, starting from the second bar
         for j=2:n_s_bars
            % Create the label string:
            %   country_x (or world)
            %   percentage
            str=sprintf('%s\n%f',v_names{j+1},x(i,j))
            % Print the label in the center of the first bar
            tx=text(date(i)-.3,sum(x(i,1:j-1))+x(i,j)/2,str)
         end
      end
      

      通过循环,生成以下图像:

      希望对你有帮助,

      卡普拉'

      【讨论】:

      • 这个T{:,2:end}./T.Total,如果你有Matlab 2016b,这不是错误。
      • 而且,我看到你真的很喜欢动画 gif :)
      猜你喜欢
      • 2019-04-10
      • 2011-09-18
      • 2021-07-03
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      相关资源
      最近更新 更多