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

2 次查看(过去 30 天)
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