How I can create a stacked Bar from Table ?

조회 수: 2 (최근 30일)
msh
msh 2017년 2월 11일
답변: Peter Perkins 2017년 2월 13일
I want to create a stacked bar chart from a table. Here there is a minimum working example of the type of table I am looking at:
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
Type of Stacked BAR
====================
Based on the T_new table I want to create a stacked bar graph. In the 'x' axis the chart should show the dates (1990,1991 etc) and for each date should be one stacked bar. So, for example, for 1990 there is should be one bar stacking the values 0.2933 0.29471 0.41199
Ideally, in the stack bar I want also to include the labels of (country1, country2, world) for the correspending values.
How I can do that in matlab ?

답변 (1개)

Peter Perkins
Peter Perkins 2017년 2월 13일
I think to get the stacked bar char you'd pass t_new.Date and t_new{:,2:end}' to the bar command.

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by