Bar chart plotting of two series

조회 수: 2 (최근 30일)
Jared
Jared 2018년 8월 18일
편집: dpb 2018년 8월 21일
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