Bar chart plotting of two series
3 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
dpb
on 18 Aug 2018
Edited: dpb
on 20 Aug 2018
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 Comments
More Answers (1)
See Also
Categories
Find more on Bar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!