【发布时间】:2020-04-09 04:56:33
【问题描述】:
% S
a1 = [2015/07/23 2015/11/25 2016/01/20];
b1 = [2011/06/22 2014/10/14 2015/03/01];
c1 = [2012/04/16 2013/06/23 2015/04/08];
d1 = [2013/09/15 2014/01/19 2016/09/13];
e1 = [2015/04/01 2016/04/04 2018/08/04];
% H
a2 = [2012/07/23 2015/06/25 2016/05/20];
b2 = [2009/06/22 2014/09/14 2015/11/01];
c2 = [2006/04/16 2013/12/23 2015/06/08];
d2 = [2008/09/15 2014/05/19 2016/02/13];
e2 = [2011/04/01 2016/05/04 2018/03/04];
% HS
a3 = [2009/07/23 2010/06/25 2018/02/20];
b3 = [2011/06/22 2014/07/14 2016/09/01];
c3 = [2013/04/16 2016/09/23 2019/05/08];
d3 = [2013/09/15 2018/05/19 2019/06/13];
e3 = [2014/04/01 2019/01/04 2019/12/04];
% T
t = [1 2 3 4 5];
dates = [a1 a2 a3; b1 b2 b3; c1 c2 c3; d1 d2 d3; e1 e2 e3];
% Plotted
figure
barh(t, dates, 'hist')
title('Script')
xlabel('Time')
ylabel('Tail')
legend({'S','H','HS'})
legend('Location', 'southoutside')
legend('Orientation','horizontal')
如果您绘制此图,您会注意到有 9 个条形图与每个“t”相关联。每个 t 的“日期”变量中应该只有三个。如何分别堆叠 'a1,a2,a3, 然后 b1,b2,b3 ...... 和 e1,e2,e3' 来完成这个壮举?
我的脚本结果:
我希望输出的样子
Note: 1. The y axis contains the 5 different elements 't'
2. The x axis should contain the date elements 'dates'
3. When you plot these values, there are 9 bars. There should be three per
't'.
4. On the x axis, I would like to have the dates represented.
5. I would like to eventually be able to create a user prompted system that
allows people to enter in dates for a corresponding array, and have that
date be stacked onto the chart.
以下代码完成了我的要求,但条形垂直而不是水平堆叠,并且还接受不同的用户输入。
https://www.mathworks.com/matlabcentral/fileexchange/32884-plot-groups-of-stacked-bars
【问题讨论】:
-
我很确定你想绘制
dates而不是d(否则你会报错,你的大部分代码都不会被使用) -
代码中的“堆叠”让我很恼火。为什么要介绍
a,b,c,d,e?在dates中,您每行连接3 个数组(每个数组由3 个元素组成,这使得dates成为一个3x3=9 列矩阵),但您只想看到3 个柱?也许你可以澄清你想要什么以及你的代码应该如何工作 -
对于 t=1,您期望 3 个柱。他们应该有什么长度?我在您的数据中看到总共 9 个数字。
-
您发布的代码在这种形式下确实没有多大意义。一半以上的变量没用!
-
您好,先生。对 Q1 的回应:是的,d=dates,请见谅。
标签: matlab