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

1 Ansicht (letzte 30 Tage)
yang-En Hsiao
yang-En Hsiao am 8 Nov. 2019
Beantwortet: Nicolas B. am 8 Nov. 2019
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?

Antworten (1)

Nicolas B.
Nicolas B. am 8 Nov. 2019
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?

Kategorien

Mehr zu Discrete Data Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by