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
댓글 수: 0
채택된 답변
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
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Bar Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!