Why can't i plot a bar figure like this example?

조회 수: 2 (최근 30일)
yang-En Hsiao
yang-En Hsiao 2019년 11월 8일
답변: Nicolas B. 2019년 11월 8일
I want to plot a bar figure like this example,just take this example as a reference
My code is as below
x=[0 5 10 15 20 25]
life=[1344 423 133 41 12 3; 939 296 93 29 9 2]
L=bar(x,life);
xtips1 = L(1).XEndPoints;
ytips1 = L(1).YEndPoints;
labels1 = string(L(1).YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
xtips2 = L(2).XEndPoints;
ytips2 = L(2).YEndPoints;
labels2 = string(L(2).YData);
text(xtips2,ytips2,labels2,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
However the window always told me
Error using bar (line 142)
The length of X must match the number of rows of Y.
Error in yesharvest_vs_noharvest (line 5)
L=bar(x,life);
I am sure that the length of x and life are both "6",but the window still told me "The length of X must match the number of rows of Y". Where am i wrong?how do i modify it?

답변 (1개)

Nicolas B.
Nicolas B. 2019년 11월 8일
If I transpose life like this:
x=[0 5 10 15 20 25]
life=[1344 423 133 41 12 3; 939 296 93 29 9 2]
L=bar(x,life');
Then I get that result:
bar_plot.PNG
Is it what you expect to get?

카테고리

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