Bar chart plotting of two series

2 次查看(过去 30 天)
Hi I have tried everything I know to try and have not been able to solve this problem. I use MATLAB to create GIFs of graphs and one particular use is creating a GIF of an incrementing bar chart with multiple Y series. For whatever reason MATLAB refuses to plot only the first X value and the first Y1 and Y2 values per the example below where I'm using two financial series as the Y1 and Y2 examples.
bar(data.Year(1),[data.SP500(1) data.EM(1)])
Error using bar (line 143)
X must be same length as Y.
If I change the example to only one Y series or two data points for X, Y1 and Y2 it works fine but it gives me the error above when trying to plot only one value for X, Y1 and Y2.
Appreciate any help,
JK

采纳的回答

dpb
dpb 2018-8-18
编辑:dpb 2018-8-20
bar(data.Year(1),[data.SP500(1) data.EM(1)])
You're concatenating [data.SP500(1) data.EM(1)] as row vector so its length is 2 and you've only got one X point so the lengths aren't commensurate as the error says.
|bar| has specific input requirements for |X,Y| as noted for the |X| variable input--
"x values, specified as a vector or a matrix. If x and y are both vectors, then they must be equal length. If x and y are both matrices, then they must be equal size. If x is a vector and y is a matrix, then the length of x must equal the number of rows in y."
Not sure what your expected chart is to look like??? Where are the two values to be with respect to the one X?
ADDENDUM
It does seem to be a bug that the input parser won't accept a one-group 'grouped' input. It came to me somewhat later that must be the effect wanted but thought I'd wait for confirmation.
Only way I've been able to work around is to augment the input array with a second row of NaN elements that don't plot.
bar([data.Year(1);nan],[data.SP500(1) data.EM(1);[nan nan]])
This is a pain, agreed and worthy of an official bug report to TMW.
Interestingly, looking at the source while it's been updated significantly, the routine goes back to C Moler 1986 and looks as though this has been a limitation from the beginning.
  2 个评论
Jared
Jared 2018-8-20
Thanks for the help. I was able to modify your code to this:
bar([data.Year(1);data.Year(2)],[data.SP500(1) data.EM(1);[nan nan]])
to produce a result that is probably the closest I can get to what I was looking to do. Since I'm incrementing this graph w/ this as the first picture in the GIF this works well.
dpb
dpb 2018-8-21
编辑:dpb 2018-8-21
I did go ahead and submit an enhancement request...we'll see if TMW thinks it's of any interest at all.
ADDENDUM
It's now in the official enhancement request database to allow either 'stacked' or 'grouped' styles for a single bar object.

请先登录,再进行评论。

更多回答(1 个)

Jared
Jared 2018-8-19
This is what first plot point of chart should look like. Two different series plotted under one X value.

类别

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